Skip to content

Commit 00fa33a

Browse files
committed
define post type for storing crawler queue
1 parent ccab5ae commit 00fa33a

File tree

6 files changed

+137
-48
lines changed

6 files changed

+137
-48
lines changed

EXTEND.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ the future or any third-party plugin to transform the data upon installation of
1515
## Storage Architecture
1616

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

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

src/plugin/class-engine.php

+7-6
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44

55
class Engine {
66

7-
public const string STORAGE_POST_TYPE = 'liberated_data';
7+
public const string LIBERATED_DATA_POST_TYPE = 'liberated_data';
8+
public const string CRAWLER_DATA_POST_TYPE = 'dl_crawler_url';
89

910
public function __construct() {
1011
require 'enum-subject-type.php';
@@ -21,15 +22,15 @@ public function __construct() {
2122
( function () {
2223
$transformer = new Transformer();
2324

24-
new Post_Type_UI( self::STORAGE_POST_TYPE, $transformer );
25+
new Post_Type_UI( self::LIBERATED_DATA_POST_TYPE, self::CRAWLER_DATA_POST_TYPE, $transformer );
2526

2627
// REST API
27-
new Blogpost_Controller( self::STORAGE_POST_TYPE );
28-
new Page_Controller( self::STORAGE_POST_TYPE );
28+
new Blogpost_Controller( self::LIBERATED_DATA_POST_TYPE );
29+
new Page_Controller( self::LIBERATED_DATA_POST_TYPE );
2930

30-
new Storage( self::STORAGE_POST_TYPE );
31+
new Storage( self::LIBERATED_DATA_POST_TYPE, self::CRAWLER_DATA_POST_TYPE );
3132

32-
Subject_Repo::init( self::STORAGE_POST_TYPE );
33+
Subject_Repo::init( self::LIBERATED_DATA_POST_TYPE );
3334
} )();
3435
}
3536
}

src/plugin/class-post-type-ui.php

+55-12
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,30 @@
33
namespace DotOrg\TryWordPress;
44

55
class Post_Type_UI {
6-
private string $post_type;
6+
private string $liberated_data_post_type;
7+
private string $crawler_data_post_type;
8+
79
private Transformer $transformer;
810

9-
public function __construct( $custom_post_type, Transformer $transformer ) {
10-
$this->post_type = $custom_post_type;
11-
$this->transformer = $transformer;
11+
public function __construct( string $liberated_data_post_type, string $crawler_data_post_type, Transformer $transformer ) {
12+
$this->liberated_data_post_type = $liberated_data_post_type;
13+
$this->crawler_data_post_type = $crawler_data_post_type;
14+
$this->transformer = $transformer;
1215

13-
$this->remove_add_new_option( $this->post_type );
16+
$this->remove_add_new_option( $this->liberated_data_post_type );
17+
$this->remove_add_new_option( $this->crawler_data_post_type );
1418

1519
// Strip editor to be barebones.
1620
add_filter(
1721
'wp_editor_settings',
1822
function ( $settings, $editor_id ) {
19-
if ( 'content' === $editor_id && get_current_screen()->post_type === $this->post_type ) {
23+
if (
24+
'content' === $editor_id &&
25+
(
26+
get_current_screen()->post_type === $this->liberated_data_post_type ||
27+
get_current_screen()->post_type === $this->crawler_data_post_type
28+
)
29+
) {
2030
$settings['tinymce'] = false;
2131
$settings['quicktags'] = false;
2232
$settings['media_buttons'] = false;
@@ -37,15 +47,23 @@ function () {
3747
$cpt_screen = false;
3848
if ( 'post-new.php' === $pagenow ) { // New post screen
3949
// @phpcs:ignore WordPress.Security.NonceVerification.Recommended
40-
if ( isset( $_GET['post_type'] ) && $_GET['post_type'] === $this->post_type ) {
50+
// @phpcs:disable
51+
if (
52+
isset( $_GET['post_type'] ) &&
53+
(
54+
$_GET['post_type'] === $this->liberated_data_post_type ||
55+
$_GET['post_type'] === $this->crawler_data_post_type
56+
)
57+
) {
4158
$cpt_screen = true;
4259
}
60+
// @phpcs:enable
4361
}
4462

4563
if ( 'post.php' === $pagenow ) { // Edit post screen
4664
// @phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.ValidatedSanitizedInput.InputNotValidated
4765
$post_type = get_post_type( absint( $_GET['post'] ) );
48-
if ( $post_type === $this->post_type ) {
66+
if ( $post_type === $this->liberated_data_post_type || $post_type === $this->crawler_data_post_type ) {
4967
$cpt_screen = true;
5068
}
5169
}
@@ -61,7 +79,7 @@ function () {
6179
add_filter(
6280
'use_block_editor_for_post_type',
6381
function ( $use_block_editor, $post_type ) {
64-
if ( $post_type === $this->post_type ) {
82+
if ( $post_type === $this->liberated_data_post_type || $post_type === $this->crawler_data_post_type ) {
6583
return false;
6684
}
6785

@@ -76,8 +94,11 @@ function ( $use_block_editor, $post_type ) {
7694
'add_meta_boxes',
7795
function () {
7896
// Remove default meta boxes
79-
remove_meta_box( 'submitdiv', $this->post_type, 'side' );
80-
remove_meta_box( 'slugdiv', $this->post_type, 'normal' );
97+
remove_meta_box( 'submitdiv', $this->liberated_data_post_type, 'side' );
98+
remove_meta_box( 'slugdiv', $this->liberated_data_post_type, 'normal' );
99+
100+
remove_meta_box( 'submitdiv', $this->crawler_data_post_type, 'side' );
101+
81102
/**
82103
* We would need to remove more metaboxes as their support is added to CPTs.
83104
* Leaving code here for reference.
@@ -116,10 +137,32 @@ function () {
116137
echo "<p>This post hasn't been transformed yet.</p>";
117138
}
118139
},
119-
$this->post_type,
140+
$this->liberated_data_post_type,
120141
'side',
121142
'default'
122143
);
144+
145+
add_meta_box(
146+
'discovered_crawler_url',
147+
'Discovered URL',
148+
function () {
149+
global $post;
150+
?>
151+
<p>
152+
<label>
153+
<input type="text" class="large-text" readonly value="<?php esc_attr( $post->guid ); ?>" />
154+
</label>
155+
</p>
156+
<p>
157+
<strong>Status:</strong>
158+
<pre><?php echo esc_html( strtoupper( $post->post_status ) ); ?></pre>
159+
</p>
160+
<?php
161+
},
162+
$this->crawler_data_post_type,
163+
'advanced',
164+
'default'
165+
);
123166
},
124167
999
125168
);

src/plugin/class-storage.php

+71-27
Original file line numberDiff line numberDiff line change
@@ -3,44 +3,88 @@
33
namespace DotOrg\TryWordPress;
44

55
class Storage {
6-
private string $post_type;
7-
private string $post_type_name;
6+
private string $liberated_data_post_type;
7+
private string $liberated_data_post_type_name;
8+
private string $crawler_data_post_type;
9+
private string $crawler_data_post_type_name = 'Crawler URL';
10+
private string $crawler_data_post_type_name_plural = 'Crawler URLs';
811

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

11-
public function __construct( string $post_type ) {
12-
$this->post_type = $post_type;
13-
$this->post_type_name = ucwords( str_replace( '_', ' ', $post_type ) );
14+
public function __construct( string $liberated_data_post_type, string $crawler_data_post_type ) {
15+
$this->liberated_data_post_type = $liberated_data_post_type;
16+
$this->liberated_data_post_type_name = ucwords( str_replace( '_', ' ', $liberated_data_post_type ) );
17+
$this->crawler_data_post_type = $crawler_data_post_type;
1418

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

18-
private function get_singular_name(): string {
19-
return $this->post_type_name;
20-
}
22+
public function register_post_types(): void {
23+
register_post_type(
24+
$this->liberated_data_post_type,
25+
array(
26+
'public' => false,
27+
'exclude_from_search' => true,
28+
'publicly_queryable' => false,
29+
'show_in_rest' => true,
30+
'show_ui' => true,
31+
'show_in_menu' => WP_DEBUG,
32+
'menu_icon' => 'dashicons-database',
33+
'supports' => $this->custom_post_types_supports,
34+
'labels' => $this->get_post_type_registration_labels(
35+
$this->liberated_data_post_type_name,
36+
$this->liberated_data_post_type_name
37+
),
38+
'rest_base' => $this->liberated_data_post_type,
39+
)
40+
);
2141

22-
private function get_plural_name(): string {
23-
return $this->post_type_name;
24-
}
42+
register_post_type(
43+
$this->crawler_data_post_type,
44+
array(
45+
'public' => false,
46+
'exclude_from_search' => true,
47+
'publicly_queryable' => false,
48+
'show_in_rest' => false,
49+
'show_ui' => true,
50+
'show_in_menu' => WP_DEBUG,
51+
'menu_icon' => 'dashicons-editor-ul',
52+
'supports' => array( '' ), // has to be empty string array, otherwise title and content support comes in by default
53+
'labels' => $this->get_post_type_registration_labels(
54+
$this->crawler_data_post_type_name,
55+
$this->crawler_data_post_type_name_plural
56+
),
57+
'rest_base' => $this->liberated_data_post_type,
58+
)
59+
);
2560

26-
public function register_post_types(): void {
27-
$name = $this->get_singular_name();
28-
$name_plural = $this->get_plural_name();
29-
30-
$args = array(
31-
'public' => false,
32-
'exclude_from_search' => true,
33-
'publicly_queryable' => false,
34-
'show_in_rest' => true,
35-
'show_ui' => true,
36-
'show_in_menu' => WP_DEBUG,
37-
'menu_icon' => 'dashicons-database',
38-
'supports' => $this->custom_post_types_supports,
39-
'labels' => $this->get_post_type_registration_labels( $name, $name_plural ),
40-
'rest_base' => $this->post_type,
61+
register_post_status(
62+
'discovered',
63+
array(
64+
'label' => _x( 'Discovered', 'post status', 'try_wordpress' ),
65+
'public' => false,
66+
'exclude_from_search' => true,
67+
'show_in_admin_all_list' => true,
68+
'show_in_admin_status_list' => true,
69+
'internal' => true,
70+
// translators: %s: Number of discovered posts
71+
'label_count' => _n_noop( 'Discovered <span class="count">(%s)</span>', 'Discovered <span class="count">(%s)</span>', 'try_wordpress' ),
72+
)
4173
);
4274

43-
register_post_type( $this->post_type, $args );
75+
register_post_status(
76+
'crawled',
77+
array(
78+
'label' => _x( 'Crawled', 'post status', 'try_wordpress' ),
79+
'public' => false,
80+
'exclude_from_search' => true,
81+
'show_in_admin_all_list' => true,
82+
'show_in_admin_status_list' => true,
83+
'internal' => true,
84+
// translators: %s: Number of crawled posts
85+
'label_count' => _n_noop( 'Crawled <span class="count">(%s)</span>', 'Crawled <span class="count">(%s)</span>', 'try_wordpress' ),
86+
)
87+
);
4488
}
4589

4690
public function get_post_type_registration_labels( string $name, string $name_plural ): array {

tests/plugin/test-storage.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,12 @@ class Storage_Test extends TestCase {
88

99
protected function setUp(): void {
1010
parent::setUp();
11-
$this->storage = new Storage( 'lib_x' );
11+
$this->storage = new Storage( 'lib_x', 'lib_crawl' );
1212
}
1313

1414
public function testRegisterPostTypes(): void {
1515
do_action( 'init' );
1616
$this->assertTrue( post_type_exists( 'lib_x' ), 'Custom post type meant for storage not registered' );
17+
$this->assertTrue( post_type_exists( 'lib_crawl' ), 'Custom post type meant for storage not registered' );
1718
}
1819
}

tests/plugin/test-transformer.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ protected function setUp(): void {
2323
'post_status' => 'draft',
2424
'post_content_filtered' => '<div><p>Content 1</p><p>Content 2</p></div>',
2525
'guid' => 'https://example.com/x',
26-
'post_type' => Engine::STORAGE_POST_TYPE,
26+
'post_type' => 'lib_x',
2727
)
2828
);
2929
update_post_meta( $this->post_id_in_db, 'subject_type', SubjectType::BLOGPOST->value );

0 commit comments

Comments
 (0)