Skip to content

Commit

Permalink
define post type for storing crawler queue
Browse files Browse the repository at this point in the history
  • Loading branch information
ashfame committed Nov 28, 2024
1 parent ccab5ae commit 00fa33a
Show file tree
Hide file tree
Showing 6 changed files with 137 additions and 48 deletions.
2 changes: 1 addition & 1 deletion EXTEND.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ the future or any third-party plugin to transform the data upon installation of
## Storage Architecture

Try WordPress stores all liberated data in a custom post type called `liberated_data`, exposed via a constant:
`\DotOrg\TryWordPress\Engine::STORAGE_POST_TYPE`.
`\DotOrg\TryWordPress\Engine::LIBERATED_DATA_POST_TYPE`.

We maintain references between the source data and transformed output using two post meta keys:

Expand Down
13 changes: 7 additions & 6 deletions src/plugin/class-engine.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

class Engine {

public const string STORAGE_POST_TYPE = 'liberated_data';
public const string LIBERATED_DATA_POST_TYPE = 'liberated_data';
public const string CRAWLER_DATA_POST_TYPE = 'dl_crawler_url';

public function __construct() {
require 'enum-subject-type.php';
Expand All @@ -21,15 +22,15 @@ public function __construct() {
( function () {
$transformer = new Transformer();

new Post_Type_UI( self::STORAGE_POST_TYPE, $transformer );
new Post_Type_UI( self::LIBERATED_DATA_POST_TYPE, self::CRAWLER_DATA_POST_TYPE, $transformer );

// REST API
new Blogpost_Controller( self::STORAGE_POST_TYPE );
new Page_Controller( self::STORAGE_POST_TYPE );
new Blogpost_Controller( self::LIBERATED_DATA_POST_TYPE );
new Page_Controller( self::LIBERATED_DATA_POST_TYPE );

new Storage( self::STORAGE_POST_TYPE );
new Storage( self::LIBERATED_DATA_POST_TYPE, self::CRAWLER_DATA_POST_TYPE );

Subject_Repo::init( self::STORAGE_POST_TYPE );
Subject_Repo::init( self::LIBERATED_DATA_POST_TYPE );
} )();
}
}
67 changes: 55 additions & 12 deletions src/plugin/class-post-type-ui.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,30 @@
namespace DotOrg\TryWordPress;

class Post_Type_UI {
private string $post_type;
private string $liberated_data_post_type;
private string $crawler_data_post_type;

private Transformer $transformer;

public function __construct( $custom_post_type, Transformer $transformer ) {
$this->post_type = $custom_post_type;
$this->transformer = $transformer;
public function __construct( string $liberated_data_post_type, string $crawler_data_post_type, Transformer $transformer ) {
$this->liberated_data_post_type = $liberated_data_post_type;
$this->crawler_data_post_type = $crawler_data_post_type;
$this->transformer = $transformer;

$this->remove_add_new_option( $this->post_type );
$this->remove_add_new_option( $this->liberated_data_post_type );
$this->remove_add_new_option( $this->crawler_data_post_type );

// Strip editor to be barebones.
add_filter(
'wp_editor_settings',
function ( $settings, $editor_id ) {
if ( 'content' === $editor_id && get_current_screen()->post_type === $this->post_type ) {
if (
'content' === $editor_id &&
(
get_current_screen()->post_type === $this->liberated_data_post_type ||
get_current_screen()->post_type === $this->crawler_data_post_type
)
) {
$settings['tinymce'] = false;
$settings['quicktags'] = false;
$settings['media_buttons'] = false;
Expand All @@ -37,15 +47,23 @@ function () {
$cpt_screen = false;
if ( 'post-new.php' === $pagenow ) { // New post screen
// @phpcs:ignore WordPress.Security.NonceVerification.Recommended
if ( isset( $_GET['post_type'] ) && $_GET['post_type'] === $this->post_type ) {
// @phpcs:disable
if (
isset( $_GET['post_type'] ) &&
(
$_GET['post_type'] === $this->liberated_data_post_type ||
$_GET['post_type'] === $this->crawler_data_post_type
)
) {
$cpt_screen = true;
}
// @phpcs:enable
}

if ( 'post.php' === $pagenow ) { // Edit post screen
// @phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.ValidatedSanitizedInput.InputNotValidated
$post_type = get_post_type( absint( $_GET['post'] ) );
if ( $post_type === $this->post_type ) {
if ( $post_type === $this->liberated_data_post_type || $post_type === $this->crawler_data_post_type ) {
$cpt_screen = true;
}
}
Expand All @@ -61,7 +79,7 @@ function () {
add_filter(
'use_block_editor_for_post_type',
function ( $use_block_editor, $post_type ) {
if ( $post_type === $this->post_type ) {
if ( $post_type === $this->liberated_data_post_type || $post_type === $this->crawler_data_post_type ) {
return false;
}

Expand All @@ -76,8 +94,11 @@ function ( $use_block_editor, $post_type ) {
'add_meta_boxes',
function () {
// Remove default meta boxes
remove_meta_box( 'submitdiv', $this->post_type, 'side' );
remove_meta_box( 'slugdiv', $this->post_type, 'normal' );
remove_meta_box( 'submitdiv', $this->liberated_data_post_type, 'side' );
remove_meta_box( 'slugdiv', $this->liberated_data_post_type, 'normal' );

remove_meta_box( 'submitdiv', $this->crawler_data_post_type, 'side' );

/**
* We would need to remove more metaboxes as their support is added to CPTs.
* Leaving code here for reference.
Expand Down Expand Up @@ -116,10 +137,32 @@ function () {
echo "<p>This post hasn't been transformed yet.</p>";
}
},
$this->post_type,
$this->liberated_data_post_type,
'side',
'default'
);

add_meta_box(
'discovered_crawler_url',
'Discovered URL',
function () {
global $post;
?>
<p>
<label>
<input type="text" class="large-text" readonly value="<?php esc_attr( $post->guid ); ?>" />
</label>
</p>
<p>
<strong>Status:</strong>
<pre><?php echo esc_html( strtoupper( $post->post_status ) ); ?></pre>
</p>
<?php
},
$this->crawler_data_post_type,
'advanced',
'default'
);
},
999
);
Expand Down
98 changes: 71 additions & 27 deletions src/plugin/class-storage.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,44 +3,88 @@
namespace DotOrg\TryWordPress;

class Storage {
private string $post_type;
private string $post_type_name;
private string $liberated_data_post_type;
private string $liberated_data_post_type_name;
private string $crawler_data_post_type;
private string $crawler_data_post_type_name = 'Crawler URL';
private string $crawler_data_post_type_name_plural = 'Crawler URLs';

private array $custom_post_types_supports = array( 'title', 'editor', 'custom-fields' );

public function __construct( string $post_type ) {
$this->post_type = $post_type;
$this->post_type_name = ucwords( str_replace( '_', ' ', $post_type ) );
public function __construct( string $liberated_data_post_type, string $crawler_data_post_type ) {
$this->liberated_data_post_type = $liberated_data_post_type;
$this->liberated_data_post_type_name = ucwords( str_replace( '_', ' ', $liberated_data_post_type ) );
$this->crawler_data_post_type = $crawler_data_post_type;

add_action( 'init', array( $this, 'register_post_types' ) );
}

private function get_singular_name(): string {
return $this->post_type_name;
}
public function register_post_types(): void {
register_post_type(
$this->liberated_data_post_type,
array(
'public' => false,
'exclude_from_search' => true,
'publicly_queryable' => false,
'show_in_rest' => true,
'show_ui' => true,
'show_in_menu' => WP_DEBUG,
'menu_icon' => 'dashicons-database',
'supports' => $this->custom_post_types_supports,
'labels' => $this->get_post_type_registration_labels(
$this->liberated_data_post_type_name,
$this->liberated_data_post_type_name
),
'rest_base' => $this->liberated_data_post_type,
)
);

private function get_plural_name(): string {
return $this->post_type_name;
}
register_post_type(
$this->crawler_data_post_type,
array(
'public' => false,
'exclude_from_search' => true,
'publicly_queryable' => false,
'show_in_rest' => false,
'show_ui' => true,
'show_in_menu' => WP_DEBUG,
'menu_icon' => 'dashicons-editor-ul',
'supports' => array( '' ), // has to be empty string array, otherwise title and content support comes in by default
'labels' => $this->get_post_type_registration_labels(
$this->crawler_data_post_type_name,
$this->crawler_data_post_type_name_plural
),
'rest_base' => $this->liberated_data_post_type,
)
);

public function register_post_types(): void {
$name = $this->get_singular_name();
$name_plural = $this->get_plural_name();

$args = array(
'public' => false,
'exclude_from_search' => true,
'publicly_queryable' => false,
'show_in_rest' => true,
'show_ui' => true,
'show_in_menu' => WP_DEBUG,
'menu_icon' => 'dashicons-database',
'supports' => $this->custom_post_types_supports,
'labels' => $this->get_post_type_registration_labels( $name, $name_plural ),
'rest_base' => $this->post_type,
register_post_status(
'discovered',
array(
'label' => _x( 'Discovered', 'post status', 'try_wordpress' ),
'public' => false,
'exclude_from_search' => true,
'show_in_admin_all_list' => true,
'show_in_admin_status_list' => true,
'internal' => true,
// translators: %s: Number of discovered posts
'label_count' => _n_noop( 'Discovered <span class="count">(%s)</span>', 'Discovered <span class="count">(%s)</span>', 'try_wordpress' ),
)
);

register_post_type( $this->post_type, $args );
register_post_status(
'crawled',
array(
'label' => _x( 'Crawled', 'post status', 'try_wordpress' ),
'public' => false,
'exclude_from_search' => true,
'show_in_admin_all_list' => true,
'show_in_admin_status_list' => true,
'internal' => true,
// translators: %s: Number of crawled posts
'label_count' => _n_noop( 'Crawled <span class="count">(%s)</span>', 'Crawled <span class="count">(%s)</span>', 'try_wordpress' ),
)
);
}

public function get_post_type_registration_labels( string $name, string $name_plural ): array {
Expand Down
3 changes: 2 additions & 1 deletion tests/plugin/test-storage.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@ class Storage_Test extends TestCase {

protected function setUp(): void {
parent::setUp();
$this->storage = new Storage( 'lib_x' );
$this->storage = new Storage( 'lib_x', 'lib_crawl' );
}

public function testRegisterPostTypes(): void {
do_action( 'init' );
$this->assertTrue( post_type_exists( 'lib_x' ), 'Custom post type meant for storage not registered' );
$this->assertTrue( post_type_exists( 'lib_crawl' ), 'Custom post type meant for storage not registered' );
}
}
2 changes: 1 addition & 1 deletion tests/plugin/test-transformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ protected function setUp(): void {
'post_status' => 'draft',
'post_content_filtered' => '<div><p>Content 1</p><p>Content 2</p></div>',
'guid' => 'https://example.com/x',
'post_type' => Engine::STORAGE_POST_TYPE,
'post_type' => 'lib_x',
)
);
update_post_meta( $this->post_id_in_db, 'subject_type', SubjectType::BLOGPOST->value );
Expand Down

0 comments on commit 00fa33a

Please sign in to comment.