Skip to content

Commit f46c20c

Browse files
authored
Merge pull request #297 from indieweb/dynamic
Dynamic Rendering
2 parents 4fe0840 + 8dbc136 commit f46c20c

10 files changed

+182
-108
lines changed

includes/class-micropub-base.php

+45-7
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,12 @@ public static function http_header() {
4949
/**
5050
* Generates webfinger/host-meta links
5151
*/
52-
public static function jrd_links( $array ) {
53-
$array['links'][] = array(
52+
public static function jrd_links( $links ) {
53+
$links['links'][] = array(
5454
'rel' => static::get_rel(),
5555
'href' => static::get_endpoint(),
5656
);
57-
return $array;
57+
return $links;
5858
}
5959

6060

@@ -79,11 +79,11 @@ public static function log_error( $message, $name = 'Micropub' ) {
7979
return error_log( sprintf( '%1$s: %2$s', $name, $message ) ); // phpcs:ignore
8080
}
8181

82-
public static function get( $array, $key, $default = array() ) {
83-
if ( is_array( $array ) ) {
84-
return isset( $array[ $key ] ) ? $array[ $key ] : $default;
82+
public static function get( $a, $key, $args = array() ) {
83+
if ( is_array( $a ) ) {
84+
return isset( $a[ $key ] ) ? $a[ $key ] : $args;
8585
}
86-
return $default;
86+
return $args;
8787
}
8888

8989
public static function load_auth() {
@@ -124,4 +124,42 @@ protected static function check_error( $result ) {
124124
}
125125
return $result;
126126
}
127+
128+
/**
129+
* Returns the mf2 properties for a post.
130+
*/
131+
public static function get_mf2( $post_id = null ) {
132+
$mf2 = array();
133+
$post = get_post( $post_id );
134+
135+
foreach ( get_post_meta( $post_id ) as $field => $val ) {
136+
$val = maybe_unserialize( $val[0] );
137+
if ( 'mf2_type' === $field ) {
138+
$mf2['type'] = $val;
139+
} elseif ( 'mf2_' === substr( $field, 0, 4 ) ) {
140+
$mf2['properties'][ substr( $field, 4 ) ] = $val;
141+
}
142+
}
143+
144+
// Time Information
145+
$published = micropub_get_post_datetime( $post );
146+
$updated = micropub_get_post_datetime( $post, 'modified' );
147+
$mf2['properties']['published'] = array( $published->format( DATE_W3C ) );
148+
if ( $published->getTimestamp() !== $updated->getTimestamp() ) {
149+
$mf2['properties']['updated'] = array( $updated->format( DATE_W3C ) );
150+
}
151+
152+
if ( ! empty( $post->post_title ) ) {
153+
$mf2['properties']['name'] = array( $post->post_title );
154+
}
155+
156+
if ( ! empty( $post->post_excerpt ) ) {
157+
$mf2['properties']['summary'] = array( htmlspecialchars_decode( $post->post_excerpt ) );
158+
}
159+
if ( ! array_key_exists( 'content', $mf2['properties'] ) && ! empty( $post->post_content ) ) {
160+
$mf2['properties']['content'] = array( htmlspecialchars_decode( $post->post_content ) );
161+
}
162+
163+
return $mf2;
164+
}
127165
}

includes/class-micropub-endpoint.php

+8-43
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,11 @@ public static function init() {
3737
add_filter( 'rest_request_after_callbacks', array( static::class, 'return_micropub_error' ), 10, 3 );
3838
}
3939

40-
public static function get( $array, $key, $default = array() ) {
41-
if ( is_array( $array ) ) {
42-
return isset( $array[ $key ] ) ? $array[ $key ] : $default;
40+
public static function get( $a, $key, $d = array() ) {
41+
if ( is_array( $a ) ) {
42+
return isset( $a[ $key ] ) ? $a[ $key ] : $d;
4343
}
44-
return $default;
44+
return $d;
4545
}
4646

4747
public static function register_route() {
@@ -986,8 +986,9 @@ public static function parse_geo_uri( $uri ) {
986986
public static function store_micropub_auth_response( $args ) {
987987
$micropub_auth_response = static::$micropub_auth_response;
988988
if ( $micropub_auth_response || ( is_assoc_array( $micropub_auth_response ) ) ) {
989-
$args['meta_input'] = mp_get( $args, 'meta_input' );
990-
$args['meta_input']['micropub_auth_response'] = wp_array_slice_assoc( $micropub_auth_response, array( 'client_id', 'client_name', 'client_icon', 'uuid' ) );
989+
$args['meta_input'] = mp_get( $args, 'meta_input' );
990+
$args['meta_input']['micropub_auth_response'] = wp_array_slice_assoc( $micropub_auth_response, array( 'client_id', 'client_name', 'client_icon', 'uuid' ) );
991+
$args['meta_input']['micropub_version']['version'] = micropub_get_plugin_version();
991992
}
992993
return $args;
993994
}
@@ -1003,8 +1004,7 @@ public static function store_micropub_auth_response( $args ) {
10031004
*/
10041005
public static function store_mf2( $args ) {
10051006
// Properties that map to WordPress properties.
1006-
// TODO: We need to still store content because the plugin adds markup to the content stored.
1007-
$excludes = array( 'name', 'published', 'updated', 'summary', 'updated' );
1007+
$excludes = array( 'name', 'published', 'updated', 'summary', 'content', 'visibility' );
10081008
$props = mp_get( static::$input, 'properties', false );
10091009
if ( ! isset( $args['ID'] ) && $props ) {
10101010
$args['meta_input'] = mp_get( $args, 'meta_input' );
@@ -1073,41 +1073,6 @@ public static function store_mf2( $args ) {
10731073
return $args;
10741074
}
10751075

1076-
/**
1077-
* Returns the mf2 properties for a post.
1078-
*/
1079-
public static function get_mf2( $post_id ) {
1080-
$mf2 = array();
1081-
$post = get_post( $post_id );
1082-
1083-
foreach ( get_post_meta( $post_id ) as $field => $val ) {
1084-
$val = maybe_unserialize( $val[0] );
1085-
if ( 'mf2_type' === $field ) {
1086-
$mf2['type'] = $val;
1087-
} elseif ( 'mf2_' === substr( $field, 0, 4 ) ) {
1088-
$mf2['properties'][ substr( $field, 4 ) ] = $val;
1089-
}
1090-
}
1091-
1092-
// Time Information
1093-
$published = micropub_get_post_datetime( $post );
1094-
$updated = micropub_get_post_datetime( $post, 'modified' );
1095-
$mf2['properties']['published'] = array( $published->format( DATE_W3C ) );
1096-
if ( $published->getTimestamp() !== $updated->getTimestamp() ) {
1097-
$mf2['properties']['updated'] = array( $updated->format( DATE_W3C ) );
1098-
}
1099-
1100-
if ( ! empty( $post->post_title ) ) {
1101-
$mf2['properties']['name'] = array( $post->post_title );
1102-
}
1103-
1104-
if ( ! empty( $post->post_excerpt ) ) {
1105-
$mf2['properties']['summary'] = array( $post->post_excerpt );
1106-
}
1107-
1108-
return $mf2;
1109-
}
1110-
11111076
/* Takes form encoded input and converts to json encoded input */
11121077
public static function form_to_json( $data ) {
11131078
$input = array();

includes/class-micropub-error.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ public function __construct( $error, $error_description, $code = 200, $debug = n
1616
}
1717
}
1818

19-
public function set_debug( $array ) {
19+
public function set_debug( $a ) {
2020
$data = $this->get_data();
21-
$this->set_data( array_merge( $data, $array ) );
21+
$this->set_data( array_merge( $data, $a ) );
2222
}
2323

2424
public function to_wp_error() {

includes/class-micropub-render.php

+41-3
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,50 @@
11
<?php
22

3-
// Post Content Filter
4-
add_filter( 'micropub_post_content', array( 'Micropub_Render', 'generate_post_content' ), 1, 2 );
53

4+
/* Generate Post Content and Save it for Pre 2.4.0 versions
5+
* add_filter( 'micropub_post_content', array( 'Micropub_Render', 'generate_post_content' ), 1, 2 );
6+
*/
7+
8+
add_filter( 'the_content', array( 'Micropub_Render', 'render_content' ), 1 );
69

710
/**
811
* Micropub Render Class
912
*/
1013
class Micropub_Render {
14+
/**
15+
* Dynamically Renders Microformats 2
16+
*
17+
*/
18+
public static function render_content( $content ) {
19+
// If this is not a micropub post return without any further work.
20+
if ( ! is_micropub_post() ) {
21+
return $content;
22+
}
23+
24+
if ( self::should_dynamic_render() ) {
25+
$input = Micropub_Base::get_mf2( get_the_ID() );
26+
return self::generate_post_content( $content, $input );
27+
}
28+
29+
return $content;
30+
}
31+
32+
public static function should_dynamic_render( $post = null ) {
33+
$post = get_post();
34+
if ( class_exists( 'Post_Kinds_Plugin' ) ) {
35+
$should = false;
36+
} else {
37+
$version = get_post_meta( $post->ID, 'micropub_version', true );
38+
if ( ! $version ) {
39+
$should = false;
40+
} elseif ( get_post_meta( $post->ID, 'mf2_content', true ) ) {
41+
$should = false;
42+
} else {
43+
$should = true;
44+
}
45+
}
46+
return apply_filters( 'micropub_dynamic_render', $should, $post );
47+
}
1148

1249
/**
1350
* Generates and returns a post_content string suitable for wp_insert_post()
@@ -70,8 +107,9 @@ public static function generate_post_content( $post_content, $input ) {
70107
}
71108
}
72109

73-
$checkin = isset( $props['checkin'] ) ? $props['checkin'][0] : null;
110+
$checkin = isset( $props['checkin'] );
74111
if ( $checkin ) {
112+
$checkin = wp_is_numeric_array( $props['checkin'] ) ? $props['checkin'][0] : $props['checkin'];
75113
$name = $checkin['properties']['name'][0];
76114
$urls = $checkin['properties']['url'];
77115
$lines[] = '<p>Checked into <a class="h-card p-location" href="' .

includes/compat-functions.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -93,10 +93,10 @@ function array_key_first( array $arr ) {
9393

9494
// Polyfill for pre-PHP 7.3.
9595
if ( ! function_exists( 'array_key_last' ) ) {
96-
function array_key_last( $array ) {
97-
if ( ! is_array( $array ) || empty( $array ) ) {
96+
function array_key_last( $a ) {
97+
if ( ! is_array( $a ) || empty( $a ) ) {
9898
return null;
9999
}
100-
return array_keys( $array )[ count( $array ) - 1 ];
100+
return array_keys( $a )[ count( $a ) - 1 ];
101101
}
102102
}

includes/functions.php

+60-36
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22

3-
function is_assoc_array( $array ) {
4-
return is_array( $array ) && array_values( $array ) !== $array;
3+
function is_assoc_array( $assoc ) {
4+
return is_array( $assoc ) && array_values( $assoc ) !== $assoc;
55
}
66

77

@@ -23,10 +23,10 @@ function getallheaders() {
2323
}
2424

2525
if ( ! function_exists( 'mp_get' ) ) {
26-
function mp_get( $array, $key, $default = array(), $index = false ) {
27-
$return = $default;
28-
if ( is_array( $array ) && isset( $array[ $key ] ) ) {
29-
$return = $array[ $key ];
26+
function mp_get( $data, $key, $def = array(), $index = false ) {
27+
$return = $def;
28+
if ( is_array( $data ) && isset( $data[ $key ] ) ) {
29+
$return = $data[ $key ];
3030
}
3131
if ( $index && wp_is_numeric_array( $return ) && ! empty( $return ) ) {
3232
$return = $return[0];
@@ -37,10 +37,10 @@ function mp_get( $array, $key, $default = array(), $index = false ) {
3737

3838
if ( ! function_exists( 'mp_filter' ) ) {
3939
// Searches for partial matches in an array of strings
40-
function mp_filter( $array, $filter ) {
40+
function mp_filter( $a, $filter ) {
4141
return array_values(
4242
array_filter(
43-
$array,
43+
$a,
4444
function ( $value ) use ( $filter ) {
4545
return ( false !== stripos( $value, $filter ) );
4646
}
@@ -55,6 +55,24 @@ function micropub_get_response() {
5555
}
5656
}
5757

58+
if ( ! function_exists( 'is_micropub_post' ) ) {
59+
function is_micropub_post( $post = null ) {
60+
$post = get_post( $post );
61+
if ( ! $post ) {
62+
return false;
63+
}
64+
$response = get_post_meta( $post->ID, 'micropub_version', true );
65+
if ( $response ) {
66+
return true;
67+
}
68+
$response = get_post_meta( $post->ID, 'micropub_auth_response', true );
69+
if ( ! $response ) {
70+
return false;
71+
}
72+
return true;
73+
}
74+
}
75+
5876
if ( ! function_exists( 'micropub_get_client_info' ) ) {
5977
function micropub_get_client_info( $post = null ) {
6078
$post = get_post( $post );
@@ -160,41 +178,47 @@ function micropub_get_post_datetime( $post = null, $field = 'date', $timezone =
160178
}
161179
}
162180

163-
function get_micropub_error( $obj ) {
164-
if ( is_array( $obj ) ) {
165-
// When checking the result of wp_remote_post
166-
if ( isset( $obj['body'] ) ) {
167-
$body = json_decode( $obj['body'], true );
168-
if ( isset( $body['error'] ) ) {
169-
return new WP_Micropub_Error(
170-
$body['error'],
171-
isset( $body['error_description'] ) ? $body['error_description'] : null,
172-
$obj['response']['code']
173-
);
181+
if ( ! function_exists( 'get_micropub_error' ) ) {
182+
function get_micropub_error( $obj ) {
183+
if ( is_array( $obj ) ) {
184+
// When checking the result of wp_remote_post
185+
if ( isset( $obj['body'] ) ) {
186+
$body = json_decode( $obj['body'], true );
187+
if ( isset( $body['error'] ) ) {
188+
return new WP_Micropub_Error(
189+
$body['error'],
190+
isset( $body['error_description'] ) ? $body['error_description'] : null,
191+
$obj['response']['code']
192+
);
193+
}
194+
}
195+
} elseif ( is_object( $obj ) && 'WP_Micropub_Error' === get_class( $obj ) ) {
196+
$data = $obj->get_data();
197+
if ( isset( $data['error'] ) ) {
198+
return $obj;
174199
}
175200
}
176-
} elseif ( is_object( $obj ) && 'WP_Micropub_Error' === get_class( $obj ) ) {
177-
$data = $obj->get_data();
178-
if ( isset( $data['error'] ) ) {
179-
return $obj;
180-
}
201+
return false;
181202
}
182-
return false;
183203
}
184204

185-
function is_micropub_error( $obj ) {
186-
return ( $obj instanceof WP_Micropub_Error );
205+
if ( ! function_exists( 'is_micropub_error' ) ) {
206+
function is_micropub_error( $obj ) {
207+
return ( $obj instanceof WP_Micropub_Error );
208+
}
187209
}
188210

189-
// Converts WP_Error into Micropub Error
190-
function micropub_wp_error( $error ) {
191-
if ( is_wp_error( $error ) ) {
192-
$data = $error->get_error_data();
193-
$status = isset( $data['status'] ) ? $data['status'] : 200;
194-
if ( is_array( $data ) ) {
195-
unset( $data['status'] );
211+
if ( ! function_exists( 'micropub_wp_error' ) ) {
212+
// Converts WP_Error into Micropub Error
213+
function micropub_wp_error( $error ) {
214+
if ( is_wp_error( $error ) ) {
215+
$data = $error->get_error_data();
216+
$status = isset( $data['status'] ) ? $data['status'] : 200;
217+
if ( is_array( $data ) ) {
218+
unset( $data['status'] );
219+
}
220+
return new WP_Micropub_Error( $error->get_error_code(), $error->get_error_message(), $status, $data );
196221
}
197-
return new WP_Micropub_Error( $error->get_error_code(), $error->get_error_message(), $status, $data );
222+
return null;
198223
}
199-
return null;
200224
}

0 commit comments

Comments
 (0)