-
Notifications
You must be signed in to change notification settings - Fork 1
/
searchwp-epub.php
90 lines (67 loc) · 2.79 KB
/
searchwp-epub.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
<?php
/*
EPUB indexer for SearchWP
@author Gregorio Pellegrino
@wordpress-plugin
Plugin Name: SearchWP EPUB
Plugin URI: https://github.com/gregoriopellegrino/searchwp-epub
Description: WordPress plugin that enables indexing of EPUBs in SearchWP
Version: 0.3.1
Author: Gregorio Pellegrino
Author URI: https://effata.it
Text Domain: search-wp
License: private
GitHub Plugin URI: https://github.com/gregoriopellegrino/searchwp-epub
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
add_filter( 'searchwp\entry\data', function( $data, \SearchWP\Entry $entry ) {
if ( 'post.attachment' !== $entry->get_source()->get_name() ) {
return $data;
}
$post = get_post($entry->get_id());
if ( 'application/epub+zip' !== $post->post_mime_type ) {
return $data;
}
if ( ! metadata_exists( 'post', $post->ID, SEARCHWP_PREFIX . 'content' ) ) {
require_once __DIR__ . '/vendor/autoload.php';
$client = \Vaites\ApacheTika\Client::make( __DIR__ .'/tika-app-1.24.1.jar' );
$filename = get_attached_file( absint( $post->ID ) );
$document_content = $client->getText( $filename );
$document_content = sanitize_text_field( $document_content );
update_post_meta( $post->ID, SEARCHWP_PREFIX . 'content', $document_content );
}
if ( !isset( $document_content ) ) {
$document_content = get_post_meta( $post->ID, SEARCHWP_PREFIX . 'content', true );
}
$metadata = (array) $client->getMetadata( $filename )->meta;
if(array_key_exists('X-Parsed-By', $metadata)) unset( $metadata['X-Parsed-By'] );
$data['document_content' ] = $document_content;
if(!array_key_exists('meta', $data)) $data['meta'] = array();
$data['meta']['searchwp_epub_metadata'] = $metadata;
return $data;
}, 10, 2 );
add_action('searchwp_index_post', function($post) {
if ( 'application/epub+zip' === $post->post_mime_type ) {
if ( ! metadata_exists( 'post', $post->ID, SEARCHWP_PREFIX . 'content' ) ) {
require __DIR__ . '/vendor/autoload.php';
$client = \Vaites\ApacheTika\Client::make( __DIR__ .'/tika-app-1.24.1.jar' );
$filename = get_attached_file( absint( $post->ID ) );
$document_content = $client->getText( $filename );
$document_content = sanitize_text_field( $document_content );
update_post_meta( $post->ID, SEARCHWP_PREFIX . 'content', $document_content );
}
}
});
add_filter( 'searchwp_get_custom_fields', function( $custom, $post_id ) {
if ( get_post_mime_type($post_id) === 'application/epub+zip' ) {
require __DIR__ . '/vendor/autoload.php';
$client = \Vaites\ApacheTika\Client::make( __DIR__ .'/tika-app-1.24.1.jar' );
$filename = get_attached_file( absint( $post->ID ) );
$metadata = (array) $client->getMetadata( $filename )->meta;
unset( $metadata['X-Parsed-By'] );
$custom['searchwp_epub_metadata'] = $metadata;
}
return $custom;
}, 10, 2);