11<?php
2- class APICouldNotSaveDocumentException extends Exception {};
2+ class APICouldNotIndexDocumentException extends Exception {};
33
44class 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}
0 commit comments