<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[10775] sites/trunk/wordpress.org/public_html/wp-content/plugins/handbook: Handbooks: Add ability to import handbook content from a remote source.</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { white-space: pre-line; overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta" style="font-size: 105%">
<dt style="float: left; width: 6em; font-weight: bold">Revision</dt> <dd><a style="font-weight: bold" href="http://meta.trac.wordpress.org/changeset/10775">10775</a><script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","description":"Review this Commit","action":{"@type":"ViewAction","url":"http://meta.trac.wordpress.org/changeset/10775","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>coffee2code</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2021-03-05 00:40:59 +0000 (Fri, 05 Mar 2021)</dd>
</dl>

<pre style='padding-left: 1em; margin: 2em 0; border-left: 2px solid #ccc; line-height: 1.25; font-size: 105%; font-family: sans-serif'>Handbooks: Add ability to import handbook content from a remote source.

* Add `WPorg_Handbook_Importer` class, incorporating related functionality from DevHub theme
* Add 'cron_interval' and 'manifest' handbook config options
* Add `get_importer()` and `init_importer()` to handbook object</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#sitestrunkwordpressorgpublic_htmlwpcontentpluginshandbookhandbookphp">sites/trunk/wordpress.org/public_html/wp-content/plugins/handbook/handbook.php</a></li>
<li><a href="#sitestrunkwordpressorgpublic_htmlwpcontentpluginshandbookinchandbookphp">sites/trunk/wordpress.org/public_html/wp-content/plugins/handbook/inc/handbook.php</a></li>
<li><a href="#sitestrunkwordpressorgpublic_htmlwpcontentpluginshandbookphpunitincludesutilsphp">sites/trunk/wordpress.org/public_html/wp-content/plugins/handbook/phpunit/includes/utils.php</a></li>
</ul>

<h3>Added Paths</h3>
<ul>
<li><a href="#sitestrunkwordpressorgpublic_htmlwpcontentpluginshandbookincimporterphp">sites/trunk/wordpress.org/public_html/wp-content/plugins/handbook/inc/importer.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="sitestrunkwordpressorgpublic_htmlwpcontentpluginshandbookhandbookphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: sites/trunk/wordpress.org/public_html/wp-content/plugins/handbook/handbook.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- sites/trunk/wordpress.org/public_html/wp-content/plugins/handbook/handbook.php    2021-03-05 00:32:53 UTC (rev 10774)
+++ sites/trunk/wordpress.org/public_html/wp-content/plugins/handbook/handbook.php      2021-03-05 00:40:59 UTC (rev 10775)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -2,6 +2,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><span class="cx" style="display: block; padding: 0 10px">  * Plugin Name: Handbook
</span><span class="cx" style="display: block; padding: 0 10px">  * Description: Features for a handbook, complete with glossary and table of contents
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Version:     2.0
</ins><span class="cx" style="display: block; padding: 0 10px">  * Author:      WordPress.org
</span><span class="cx" style="display: block; padding: 0 10px">  * Author URI:  https://wordpress.org/
</span><span class="cx" style="display: block; padding: 0 10px">  * License:     GPLv2 or later
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -24,3 +25,8 @@
</span><span class="cx" style="display: block; padding: 0 10px"> require_once __DIR__ . '/inc/watchlist.php';
</span><span class="cx" style="display: block; padding: 0 10px"> require_once __DIR__ . '/inc/blocks.php';
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+add_action( 'plugins_loaded', function () {
+       if ( class_exists( 'WordPressdotorg\\Markdown\\Importer' ) ) {
+               require_once __DIR__ . '/inc/importer.php';
+       }
+}, 1 );
</ins></span></pre></div>
<a id="sitestrunkwordpressorgpublic_htmlwpcontentpluginshandbookinchandbookphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: sites/trunk/wordpress.org/public_html/wp-content/plugins/handbook/inc/handbook.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- sites/trunk/wordpress.org/public_html/wp-content/plugins/handbook/inc/handbook.php        2021-03-05 00:32:53 UTC (rev 10774)
+++ sites/trunk/wordpress.org/public_html/wp-content/plugins/handbook/inc/handbook.php  2021-03-05 00:40:59 UTC (rev 10775)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -36,6 +36,11 @@
</span><span class="cx" style="display: block; padding: 0 10px">        protected $label = '';
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         * The associated importer object, if warranted.
+        */
+       protected $importer;
+
+       /**
</ins><span class="cx" style="display: block; padding: 0 10px">          * Returns the custom handbook-related capabilities granted to site users.
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><span class="cx" style="display: block; padding: 0 10px">         * @return array
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -77,15 +82,23 @@
</span><span class="cx" style="display: block; padding: 0 10px">                 * @param array $config {
</span><span class="cx" style="display: block; padding: 0 10px">                 *     Associative array of handbook configuration.
</span><span class="cx" style="display: block; padding: 0 10px">                 *
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 *     @type string $cron_interval The cron interval for which an imported handbook gets
+                *                                 imported, e.g. 'hourly', 'daily'. If defined as an
+                *                                 unrecognized interval, 'hourly' will be used.
+                *                                 Default '15_minutes'.
</ins><span class="cx" style="display: block; padding: 0 10px">                  *     @type string $label The label for the handbook. Default is the
</span><span class="cx" style="display: block; padding: 0 10px">                 *                         post type slug converted to titlecase (e.g.
</span><span class="cx" style="display: block; padding: 0 10px">                 *                         plugin-handbok => "Plugin Handbook").
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 *     @type string manifest       The URL to the manifest JSON file for an imported
+                *                                 handbook.
</ins><span class="cx" style="display: block; padding: 0 10px">                  *     @type string $slug  The slug for the post type. Default is the
</span><span class="cx" style="display: block; padding: 0 10px">                 *                         post type.
</span><span class="cx" style="display: block; padding: 0 10px">                 * }
</span><span class="cx" style="display: block; padding: 0 10px">                 */
</span><span class="cx" style="display: block; padding: 0 10px">                return (array) apply_filters( 'handbook_default_handbook_config', [
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                        'cron_interval' => '15_minutes',
</ins><span class="cx" style="display: block; padding: 0 10px">                         'label'         => '',
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                        'manifest'      => '',
</ins><span class="cx" style="display: block; padding: 0 10px">                         'slug'          => '',
</span><span class="cx" style="display: block; padding: 0 10px">                ] );
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -141,6 +154,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                $this->setting_name = $this->post_type . '_name';
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                add_action( 'after_handbooks_init',               [ $this, 'init_importer' ] );
</ins><span class="cx" style="display: block; padding: 0 10px">                 add_filter( 'user_has_cap',                       [ $this, 'grant_handbook_caps' ] );
</span><span class="cx" style="display: block; padding: 0 10px">                add_action( 'widgets_init',                       [ $this, 'register_post_type' ] );
</span><span class="cx" style="display: block; padding: 0 10px">                add_filter( 'post_type_link',                     [ $this, 'post_type_link' ], 10, 2 );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -173,6 +187,32 @@
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         * Returns the handbook's importer object, if applicable.
+        *
+        * @return WPorg_Handbook_Importer|null
+        */
+       public function get_importer() {
+               return $this->importer;
+       }
+
+       /**
+        * Initializes the importer, if applicable.
+        */
+       public function init_importer() {
+               $config = $this->get_config();
+
+               if ( class_exists( 'WPorg_Handbook_Importer' ) ) {
+                       if ( WPorg_Handbook_Importer::is_handbook_imported( $this->post_type ) ) {
+                               $this->importer = new WPorg_Handbook_Importer( $this );
+                       }
+               } elseif ( is_admin() && ( $config['manifest'] ?: false ) ) {
+                       add_action( 'admin_notices', function () {
+                               echo '<div class="notice notice-error"><p>' . __( 'Error: The <strong>WPORG Markdown Importer</strong> plugin needs to be activated in order to allow importing of handbooks.', 'wporg' ) . '</p></div>';
+                       } );
+               }
+       }
+
+       /**
</ins><span class="cx" style="display: block; padding: 0 10px">          * Adds 'Handbook Front Page' post state indicator for handbook landing pages.
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><span class="cx" style="display: block; padding: 0 10px">         * @param string[] $post_states An array of post display states.
</span></span></pre></div>
<a id="sitestrunkwordpressorgpublic_htmlwpcontentpluginshandbookincimporterphp"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: sites/trunk/wordpress.org/public_html/wp-content/plugins/handbook/inc/importer.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- sites/trunk/wordpress.org/public_html/wp-content/plugins/handbook/inc/importer.php                                (rev 0)
+++ sites/trunk/wordpress.org/public_html/wp-content/plugins/handbook/inc/importer.php  2021-03-05 00:40:59 UTC (rev 10775)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,203 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+<?php
+/**
+ * Class providing handbook import functionality.
+ *
+ * @package handbook
+ */
+
+use WordPressdotorg\Markdown\Editor;
+use WordPressdotorg\Markdown\Importer;
+
+class WPorg_Handbook_Importer extends Importer {
+
+       /**
+        * Singleton instance.
+        *
+        * @var static
+        */
+       protected static $instances;
+
+       /**
+        * The slug base.
+        *
+        * @var string
+        */
+       protected $base;
+
+       /**
+        * The manifest URL.
+        *
+        * E.g. https://raw.githubusercontent.com/WP-API/docs/master/bin/manifest.json
+        *
+        * @var string
+        */
+       protected $manifest_url;
+
+       /**
+        * The cron update interval schedule.
+        *
+        * @var string
+        */
+       protected $cron_interval = '15_minutes';
+
+       /**
+        * Get the singleton instance, or create if needed.
+        *
+        * @return WPorg_Handbook_Importer
+        */
+       public static function instance( $post_type ) {
+               return $instances[ $post_type ] ?? null;
+       }
+
+       /**
+        * Returns the base URL for the handbook.
+        *
+        * @return string
+        */
+       protected function get_base() {
+               return home_url( "{$this->base}/" );
+       }
+
+       /**
+        * Returns the manifest URL.
+        *
+        * @return string
+        */
+       protected function get_manifest_url() {
+               return $this->manifest_url;
+       }
+
+       /**
+        * Returns the post type for the imported handbook.
+        *
+        * @param bool $omit_suffix Return only the base of the post type name (i.e.
+        *                          without the '-handbook' suffix). Default false.
+        * @return string
+        */
+       public function get_post_type( $omit_suffix = false ) {
+               return $omit_suffix ? str_replace( '-handbook', '', $this->handbook->post_type ) : $this->handbook->post_type;
+       }
+
+       /**
+        * Initializes the object.
+        *
+* REDO THIS WHOLE DESCRIPTION
+        * @param string $manifest_url The manifest URL.
+        * @param array  $args         {
+        *     Optional. An array of configuration options.
+        *
+        *     @type bool   $github_edit Override edit links to link to GitHub source? Default true.
+        *     @type string $post_type   The post type base. Hypenated and without 'handbook'
+        *                               (unless it is 'handbook'). Default 'handbook'.
+        *     @type string $slug        The slug for the post type. Default is post type.
+        * }
+        */
+       public function __construct( $handbook ) {
+               if ( ! is_a( $handbook, 'WPorg_Handbook' ) ) {
+                       return new WP_Error( 'not_a_handbook', __( 'Handbook importer initialized with an invalid handbook.', 'wporg' ) );
+               }
+
+               // Get previously instantiated version.
+               if ( $instance = self::instance( $handbook->post_type ) ) {
+                       return $instance;
+               }
+
+               $this->handbook = $handbook;
+
+               $config = $this->handbook->get_config();
+               $post_type_base = $this->get_post_type( true );
+
+               self::$instances[ $this->get_post_type() ] = $this;
+
+               $this->base         = $config['slug'];
+               $this->manifest_url = $config['manifest'];
+
+               add_filter( 'cron_schedules',                                 [ $this, 'filter_cron_schedules' ] );
+               add_action( 'init',                                           [ $this, 'register_cron_jobs' ] );
+               add_action( "handbook_{$post_type_base}_import_manifest",     [ $this, 'import_manifest' ] );
+               add_action( "handbook_{$post_type_base}_import_all_markdown", [ $this, 'import_all_markdown' ] );
+               add_filter( 'display_post_states',                            [ $this, 'display_post_states' ], 10, 2 );
+
+               $editor = new Editor( $this );
+               $editor->init();
+       }
+
+       /**
+        * Returns the handbook's cron interval schedule.
+        *
+        * @return string
+        */
+       public function get_cron_interval() {
+               $cron_intervals = wp_get_schedules();
+               $default = 'hourly';
+
+               $cron_interval = $this->handbook->get_config()['cron_interval'] ?: $default;
+
+               return $cron_intervals[ $cron_interval ] ?? $cron_intervals[ $default ];
+       }
+
+       /**
+        * Adds 'Not Imported' post state indicator for handbook pages that aren't
+        * the result of an import.
+        *
+        * @param string[] $post_states An array of post display states.
+        * @param WP_Post  $post        The current post object.
+        * @return string[]
+        */
+       public function display_post_states( $post_states, $post ) {
+               $post_type = get_post_type( $post );
+
+               if ( self::is_handbook_imported( $post_type ) && ! get_post_meta( $post->ID, $this->manifest_entry_meta_key, true ) ) {
+                       $post_states[] = __( 'Not Imported', 'wporg' );
+               }
+
+               return $post_states;
+       }
+
+       /**
+        * Filters cron schedules to add a 15 minute schedule, if there isn't one.
+        *
+        * @param array $schedules Cron schedules.
+        * @return array
+        */
+       public function filter_cron_schedules( $schedules ) {
+               if ( empty( $schedules['15_minutes'] ) ) {
+                       $schedules['15_minutes'] = array(
+                               'interval' => 15 * MINUTE_IN_SECONDS,
+                               'display'  => 'every 15 minutes'
+                       );
+               }
+
+               return $schedules;
+       }
+
+       /**
+        * Registers cron jobs.
+        */
+       public function register_cron_jobs() {
+               $cron_interval = $this->get_cron_interval();
+
+               $post_type_base = $this->get_post_type( true );
+
+               if ( ! wp_next_scheduled( "handbook_{$post_type_base}_import_manifest" ) ) {
+                       wp_schedule_event( time(), $cron_interval, "handbook_{$post_type_base}_import_manifest" );
+               }
+
+               if ( ! wp_next_scheduled( "handbook_{$post_type_base}_import_all_markdown" ) ) {
+                       wp_schedule_event( time(), $cron_interval, "handbook_{$post_type_base}_import_all_markdown" );
+               }
+       }
+
+       /**
+        * Indicates if the specified handbook is imported.
+        *
+        * @param string $post_type Handbook post type.
+        * @return bool True is handbook is imported, else false.
+        */
+       public static function is_handbook_imported( $post_type ) {
+               $config = WPorg_Handbook_Init::get_handbooks_config( $post_type );
+
+               return ! empty( $config['manifest'] );
+       }
+
+}
</ins></span></pre></div>
<a id="sitestrunkwordpressorgpublic_htmlwpcontentpluginshandbookphpunitincludesutilsphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: sites/trunk/wordpress.org/public_html/wp-content/plugins/handbook/phpunit/includes/utils.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- sites/trunk/wordpress.org/public_html/wp-content/plugins/handbook/phpunit/includes/utils.php      2021-03-05 00:32:53 UTC (rev 10774)
+++ sites/trunk/wordpress.org/public_html/wp-content/plugins/handbook/phpunit/includes/utils.php        2021-03-05 00:40:59 UTC (rev 10775)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -31,7 +31,9 @@
</span><span class="cx" style="display: block; padding: 0 10px">  */
</span><span class="cx" style="display: block; padding: 0 10px"> function dataprovider_get_default_config() {
</span><span class="cx" style="display: block; padding: 0 10px">        return [
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                [ 'cron_interval', '15_minutes' ],
</ins><span class="cx" style="display: block; padding: 0 10px">                 [ 'label', '' ],
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                [ 'manifest', '' ],
</ins><span class="cx" style="display: block; padding: 0 10px">                 [ 'slug', '' ],
</span><span class="cx" style="display: block; padding: 0 10px">        ];
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span></span></pre>
</div>
</div>

</body>
</html>