Skip to content
This repository was archived by the owner on Sep 18, 2019. It is now read-only.

Commit 28b9b34

Browse files
author
Joshua K. Farrar
committed
ensure that unpublished posts are properly unindexed
1 parent 740cab2 commit 28b9b34

File tree

5 files changed

+115
-71
lines changed

5 files changed

+115
-71
lines changed

class.digitalgov_search-api.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
class DigitalGov_Search_API {
4+
5+
public static $API_URL = 'https://i14y.usa.gov/api/v1/documents';
6+
7+
public static function index_new_document($document) {
8+
$url = self::$API_URL;
9+
$credentials = DigitalGov_Search::get_handle() .":". DigitalGov_Search::get_token();
10+
$headers = array(
11+
'headers' => array(
12+
'Authorization' => 'Basic '.base64_encode( $credentials )
13+
),
14+
'method' => 'POST',
15+
'body' => $document
16+
);
17+
$headers['method'] = 'POST';
18+
19+
return wp_remote_request( $url, $headers );
20+
}
21+
22+
public static function update_existing_document($document) {
23+
$url = self::$API_URL . "/{$document->document_id}";
24+
$credentials = DigitalGov_Search::get_handle() .":". DigitalGov_Search::get_token();
25+
$headers = array(
26+
'headers' => array(
27+
'Authorization' => 'Basic '.base64_encode( $credentials )
28+
),
29+
'method' => 'PUT',
30+
'body' => $document
31+
);
32+
33+
return wp_remote_request( $url, $headers );
34+
}
35+
36+
public static function unindex_document($document) {
37+
$url = "https://i14y.usa.gov/api/v1/documents/{$document->document_id}";
38+
39+
// headers
40+
$credentials = DigitalGov_Search::get_handle() .":". DigitalGov_Search::get_token();
41+
$headers = array(
42+
'headers' => array(
43+
'Authorization' => 'Basic '.base64_encode( $credentials )
44+
),
45+
'method' => 'DELETE'
46+
);
47+
48+
return wp_remote_request( $url, $headers );
49+
}
50+
}
Lines changed: 32 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?php
2-
class APICouldNotSaveDocumentException extends Exception {};
2+
class APICouldNotIndexDocumentException extends Exception {};
33

44
class DigitalGov_Search_Document {
55
public $document_id; // required
@@ -11,10 +11,11 @@ class DigitalGov_Search_Document {
1111

1212
public static $DOCUMENT_CREATED = 0;
1313
public static $DOCUMENT_UPDATED = 1;
14-
public static $DOCUMENT_INDEX_ERROR = 2;
15-
public static $API_ERROR = 3;
14+
public static $API_ERROR = 2;
15+
public static $DOCUMENT_UNINDEXED = 3;
1616

1717
private static $ALREADY_INDEXED = 'digitalgov_search_indexed';
18+
private static $DOCUMENT_ID = 'digitalgov_search_document_id';
1819

1920
public static function create_from_post( WP_POST $post ) {
2021
$document = new self;
@@ -25,23 +26,15 @@ public static function create_from_post( WP_POST $post ) {
2526
public function populate_from_post( WP_POST $post ) {
2627
// use document ids like `blog-title-123`
2728
// if the blog name changes, this will break indexing, implicitly reindexing everything
28-
$this->document_id = $post->ID;
29+
$this->document_id = self::get_document_id($post->ID);
2930
$this->title = $post->post_title;
30-
$this->path = get_permalink( $post->ID );
31+
$this->path = self::apply_url_filters(get_permalink( $post->ID ));
3132
$this->created = $post->post_date;
3233
$this->content = $post->post_content;
3334
$this->changed = $post->post_modified;
3435
}
3536

36-
public function set_id($id) {
37-
$this->document_id = $id;
38-
}
39-
40-
public function set_path($path) {
41-
$this->path = $path;
42-
}
43-
44-
public static function filter_url($url) {
37+
public static function apply_url_filters($url) {
4538
$parsed_url = parse_url($url);
4639

4740
$remove_edit_from_host = function($host) {
@@ -53,68 +46,53 @@ public static function filter_url($url) {
5346
return "{$parsed_url['scheme']}://{$parsed_url['host']}{$parsed_url['path']}";
5447
}
5548

56-
public function save() {
57-
$already_indexed = $this->already_indexed();
5849

59-
// url
60-
$url = 'https://i14y.usa.gov/api/v1/documents';
61-
if ( $already_indexed ) {
62-
$url .= "/{$this->document_id}";
63-
}
50+
public function index( $post_id ) {
51+
$already_indexed = self::already_indexed( $post_id );
6452

65-
$obj = clone $this;
66-
$obj->set_id(sanitize_title(get_bloginfo()) . "-" . $this->document_id);
67-
$obj->set_path(self::filter_url($this->path));
68-
69-
// headers
70-
$credentials = DigitalGov_Search::get_handle() .":". DigitalGov_Search::get_token();
71-
$headers = array(
72-
'headers' => array(
73-
'Authorization' => 'Basic '.base64_encode( $credentials )
74-
),
75-
'body' => $obj
76-
);
77-
$headers['method'] = ( $already_indexed ) ? 'PUT' : 'POST';
78-
79-
$res = wp_remote_request( $url, $headers );
53+
$res = ( $already_indexed ) ? DigitalGov_Search_API::update_existing_document($this) : DigitalGov_Search_API::index_new_document($this);
8054
if ( is_a( $res, 'WP_ERROR' ) ) {
8155
return self::$API_ERROR;
8256
}
8357

8458
if ( $res['response']['code'] == 201 ) {
85-
update_post_meta( $this->document_id, self::$ALREADY_INDEXED, true );
59+
update_post_meta( $post_id, self::$ALREADY_INDEXED, true );
60+
update_post_meta( $post_id, self::$DOCUMENT_ID, $this->document_id );
8661
return self::$DOCUMENT_CREATED;
8762
} elseif ($res['response']['code'] == 200) {
63+
update_post_meta( $post_id, self::$ALREADY_INDEXED, true );
8864
return self::$DOCUMENT_UPDATED;
8965
} else {
9066
$body = json_decode($res['body']);
91-
throw new APICouldNotSaveDocumentException($body->developer_message);
67+
throw new APICouldNotIndexDocumentException($body->developer_message);
9268
}
9369
}
9470

95-
public function already_indexed() {
96-
return get_post_meta( $this->document_id, self::$ALREADY_INDEXED, true );
71+
public static function already_indexed( $post_id ) {
72+
return get_post_meta( $post_id, self::$ALREADY_INDEXED, true );
9773
}
9874

99-
public function delete() {
100-
if ( ! $this->already_indexed() ) {
101-
return false;
75+
public static function get_document_id( $post_id ) {
76+
if (self::already_indexed($post_id)) {
77+
return get_post_meta( $post_id, self::$DOCUMENT_ID, true );
78+
} else {
79+
return self::create_document_id($post_id);
10280
}
81+
}
82+
83+
public function create_document_id($post_id) {
84+
return sanitize_title(get_bloginfo()) . "-" . $post_id;
85+
}
10386

104-
$url = "https://i14y.usa.gov/api/v1/documents/{$this->document_id}";
87+
public function unindex( $post_id ) {
88+
if ( ! self::already_indexed( $post_id ) ) {
89+
return false;
90+
}
10591

106-
// headers
107-
$credentials = DigitalGov_Search::get_handle() .":". DigitalGov_Search::get_token();
108-
$headers = array(
109-
'headers' => array(
110-
'Authorization' => 'Basic '.base64_encode( $credentials )
111-
),
112-
'method' => 'DELETE'
113-
);
92+
DigitalGov_Search_API::unindex_document($this);
11493

115-
wp_remote_request( $url, $headers );
11694
delete_post_meta( $this->document_id, self::$ALREADY_INDEXED );
11795

118-
return $this;
96+
return self::$DOCUMENT_UNINDEXED;
11997
}
12098
}

class.digitalgov_search.php

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,33 @@ private static function failure_to_activate( $message ) {
2323

2424
public static function update_post( $post_id ) {
2525
$post = get_post( $post_id );
26+
$document = DigitalGov_Search_Document::create_from_post( $post );
2627
if ( $post->post_status == 'publish' ) {
2728
try {
28-
$document = DigitalGov_Search_Document::create_from_post( $post );
29-
$document->save();
30-
} catch (APICouldNotSaveDocumentException $e) {
29+
$document->index( $post_id );
30+
} catch (APICouldNotIndexDocumentException $e) {
3131
update_option('digitalgov_search_admin_message', $e->getMessage());
3232
update_option('digitalgov_search_display_post_error_message', 1);
3333
}
3434
}
3535
}
3636

37+
public static function unindex_post_when_unpublished($new_status, $old_status, $post) {
38+
if ( $old_status == 'publish' && $new_status != 'publish' ) {
39+
$document = DigitalGov_Search_Document::create_from_post( $post );
40+
try {
41+
$document->unindex( $post->ID );
42+
} catch (Exception $e) {
43+
}
44+
}
45+
}
46+
47+
public static function delete_post( $post_id ) {
48+
$post = get_post( $post_id );
49+
$document = DigitalGov_Search_Document::create_from_post( $post );
50+
$document->unindex();
51+
}
52+
3753
public static function view( $name ) {
3854
$file = DIGITALGOV_SEARCH__PLUGIN_DIR . 'views/'. $name . '.php';
3955
include( $file );

digitalgov_search.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,12 @@
1818

1919
require_once( DIGITALGOV_SEARCH__PLUGIN_DIR . 'class.digitalgov_search.php' );
2020
require_once( DIGITALGOV_SEARCH__PLUGIN_DIR . 'class.digitalgov_search-document.php' );
21+
require_once( DIGITALGOV_SEARCH__PLUGIN_DIR . 'class.digitalgov_search-api.php' );
2122

2223
add_action( 'save_post', array( 'DigitalGov_Search', 'update_post' ) );
24+
add_action( 'before_delete_post', array( 'DigitalGov_Search', 'delete_post' ) );
2325
add_action( 'admin_head-post.php', array( 'DigitalGov_Search_Admin', 'add_indexer_notice' ) );
26+
add_action( 'transition_post_status', array( 'DigitalGov_Search', 'unindex_post_when_unpublished' ), 10, 3 );
2427

2528
if ( is_admin() ) {
2629
require_once( DIGITALGOV_SEARCH__PLUGIN_DIR . 'class.digitalgov_search-admin.php' );

views/index.php

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
<h2>DigitalGov Search</h2>
2-
<p>DigitalGov Search is used to index your agency's WordPress website.</p>
1+
<h2>DigitalGov Search i14y Indexer</h2>
2+
<p>DigitalGov Search is used to index your agency's WordPress website's content with the DigitalGov Search search service.</p>
33

44
<pre>
55
<?php
@@ -13,35 +13,32 @@ class WordPressCouldNotConnectToAPIException extends Exception {};
1313
);
1414
$posts_array = get_posts( $args );
1515

16-
function saveDocument($document) {
17-
switch( $document->save() ) {
16+
function indexDocument($post_id, $document) {
17+
switch( $document->index( $post_id ) ) {
1818
case $document::$DOCUMENT_CREATED:
1919
$status = "Created document";
2020
break;
2121
case $document::$DOCUMENT_UPDATED:
2222
$status = "Updated document";
2323
break;
24-
case $document::$DOCUMENT_INDEX_ERROR:
25-
$status = "Failed to index";
26-
break;
2724
case $document::$API_ERROR:
2825
$status = "API Error: Failed to index";
2926
throw new WordPressCouldNotConnectToAPIException($status);
3027
break;
3128
}
32-
echo "{$status}: {$document->title}" . "\n";
29+
echo "<span style=\"color:#7ad03a\">{$status} \"{$document->title}\" (post id {$document->document_id})" . "</span>\n";
3330
}
3431

35-
function attemptToSaveDocument($document, $attempt) {
32+
function attemptToIndexDocument($post_id, $document, $attempt) {
3633
try {
37-
saveDocument($document);
38-
} catch (APICouldNotSaveDocumentException $e) {
39-
echo "Error saving document: {$e->getMessage()}";
34+
indexDocument($post_id, $document);
35+
} catch (APICouldNotIndexDocumentException $e) {
36+
echo "<span style=\"color:#dd3d36\">Error indexing document \"{$document->title}\": {$e->getMessage()}</span>\n";
4037
} catch (WordPressCouldNotConnectToAPIException $e) {
4138
echo "{$e->getMessage()} {$document->title}.";
4239
if (++$attempt < $MAX_ATTEMPTS_PER_POST) {
4340
echo " Trying again...";
44-
attemptToSaveDocument($document, $attempt);
41+
attemptToIndexDocument($post_id, $document, $attempt);
4542
}
4643
echo "\n";
4744
}
@@ -50,7 +47,7 @@ function attemptToSaveDocument($document, $attempt) {
5047
foreach($posts_array as $post) {
5148
try {
5249
$document = DigitalGov_Search_Document::create_from_post( $post );
53-
attemptToSaveDocument($document, 0);
50+
attemptToIndexDocument($post->ID, $document, 0);
5451
} catch (Exception $e) {
5552
echo "Unknown Exception: {$e->getMessage()}";
5653
}

0 commit comments

Comments
 (0)