-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathembed-piwigo.php
More file actions
135 lines (130 loc) · 5.16 KB
/
embed-piwigo.php
File metadata and controls
135 lines (130 loc) · 5.16 KB
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
<?php
/**
* The Embed Piwigo plugin adds support for embedding photos from whitelisted Piwigo websites.
*
* @file
* @package piwigo-embed
* @since 0.1.0
*
* @wordpress-plugin
* Plugin Name: Embed Piwigo
* Plugin URI: https://samwilson.id.au/plugins/embed-piwigo/
* Description: Embed photos from a whitelist of Piwigo websites.
* Version: 1.0.1
* Author: Sam Wilson
* Author URI: https://samwilson.id.au
* License: GPL-2.0+
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt
* Text Domain: embed-piwigo
* Domain Path: /languages
*/
// If this file is called directly, abort.
if ( ! defined( 'WPINC' ) ) {
die;
}
// Load the URLs and register embed handlers for them all.
$base_urls = array_filter( explode( "\n", get_option( 'embed-piwigo-urls' ) ) );
foreach ( $base_urls as $i => $base_url ) {
$trimmed_base_url = trim( $base_url, "\t\n\r\0\x0B/" );
wp_embed_register_handler(
"piwigo_$i",
"|($trimmed_base_url)/picture[^0-9]*([0-9]+)|i",
function ( $matches, $attr, $url, $rawattr ) {
$base_url = $matches[1];
$image_id = $matches[2];
try {
$info = embed_piwigo_get_image_info( $base_url, $image_id );
} catch ( Exception $exception ) {
// translators: a prefix to add to error messages.
$msg = __( 'Error: %s', 'embed-piwigo' );
return '<p class="embed-piwigo error">' . sprintf( $msg, $exception->getMessage() ) . '</p>';
}
$medium = $info['derivatives']['medium'];
$image_url = $medium['url'];
$title = $info['name'];
$date = $info['date_creation']
? date_i18n( get_option( 'date_format' ), strtotime( $info['date_creation'] ) )
: false;
$description = $info['comment'];
$link_format = '<a href="%s"><img src="%s" alt="%s" /></a>';
$img_link = sprintf( $link_format, $url, $image_url, $title );
$caption_attrs = [
'caption' => embed_piwigo_format_caption( $title, $date, $description ),
'width' => $medium['width'],
'align' => 'aligncenter',
];
return img_caption_shortcode( $caption_attrs, $img_link );
}
);
}
// Add the "Piwigo site URLs" option to the end of the general options page.
add_action(
'admin_init', function () {
$option_group = 'writing';
register_setting( $option_group, 'embed-piwigo-urls', [ 'type' => 'string' ] );
add_settings_field(
'embed-piwigo-urls',
__( 'Piwigo site URLs', 'embed-piwigo' ),
function ( $args ) {
$val = get_option( 'embed-piwigo-urls' );
echo '<textarea id="embed-piwigo-urls" name="embed-piwigo-urls" cols="80" rows="3">' . esc_html( $val ) . '</textarea>';
// translators: help text for this plugin's configuration option.
echo '<p class="description">' . esc_html( __( 'Base URLs of Piwigo sites, one per line.', 'embed-piwigo' ) ) . '</p>';
},
$option_group
); }
);
/**
* Format an HTML caption.
*
* @param string $title The photo title.
* @param string $date The photo date (already formatted).
* @param string $description The photo description.
* @return string
*/
function embed_piwigo_format_caption( $title, $date, $description ) {
$caption = '';
// Format the caption depending on what metadata is present.
if ( $title && $date && $description ) {
$caption = sprintf( '<strong>%1$s (%2$s):</strong> %3$s', $title, $date, $description );
} elseif ( $title && $date ) {
$caption = sprintf( '<strong>%1$s (%2$s)</strong>', $title, $date );
} elseif ( $date && $description ) {
$caption = sprintf( '<strong>%1$s:</strong> %2$s', $date, $description );
} elseif ( $title && $description ) {
$caption = sprintf( '<strong>%1$s:</strong> %2$s', $title, $description );
}
return $caption;
}
/**
* Get information from a Piwigo site about a single image.
* This function takes care of caching, and will only request new information every hour at most (unless we're in debug mode).
*
* @param string $base_url The base URL for the Piwigo site.
* @param int $image_id The Piwigo ID of the image to get information for.
* @return string[][][]
* @throws Exception If no data could be retrieved.
*/
function embed_piwigo_get_image_info( $base_url, $image_id ) {
$api_url = "$base_url/ws.php?format=json&method=pwg.images.getInfo&image_id=$image_id";
$transient_name = 'embed_piwigo_site_' . md5( $base_url ) . '_' . $image_id;
$cached = get_transient( $transient_name );
if ( $cached && ! WP_DEBUG ) {
return $cached;
}
$response = wp_remote_get( $api_url );
if ( $response instanceof WP_Error ) {
// translators: error message displayed when no response could be got from a Piwigo API call.
$msg = __( 'Unable to retrieve photo %s', 'embed-piwigo' );
throw new Exception( sprintf( $msg, $image_id ) );
} else {
$info = json_decode( $response['body'], true );
if ( ! isset( $info['result'] ) && isset( $info['message'] ) ) {
// translators: error message displayed when an error was received from a Piwigo API call.
$msg = __( 'Unable to retrieve photo %1$s (Piwigo said: %2$s)', 'embed-piwigo' );
throw new Exception( sprintf( $msg, $image_id, $info['message'] ) );
}
set_transient( $transient_name, $info['result'], 60 * 60 );
return $info['result'];
}
}