Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dynamic Rendering #297

Merged
merged 9 commits into from
Jun 6, 2024
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 45 additions & 7 deletions includes/class-micropub-base.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,12 @@ public static function http_header() {
/**
* Generates webfinger/host-meta links
*/
public static function jrd_links( $array ) {
$array['links'][] = array(
public static function jrd_links( $links ) {
$links['links'][] = array(
'rel' => static::get_rel(),
'href' => static::get_endpoint(),
);
return $array;
return $links;
}


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

public static function get( $array, $key, $default = array() ) {
if ( is_array( $array ) ) {
return isset( $array[ $key ] ) ? $array[ $key ] : $default;
public static function get( $a, $key, $args = array() ) {
if ( is_array( $a ) ) {
return isset( $a[ $key ] ) ? $a[ $key ] : $args;
}
return $default;
return $args;
}

public static function load_auth() {
Expand Down Expand Up @@ -124,4 +124,42 @@ protected static function check_error( $result ) {
}
return $result;
}

/**
* Returns the mf2 properties for a post.
*/
public static function get_mf2( $post_id = null ) {
$mf2 = array();
$post = get_post( $post_id );

foreach ( get_post_meta( $post_id ) as $field => $val ) {
$val = maybe_unserialize( $val[0] );
if ( 'mf2_type' === $field ) {
$mf2['type'] = $val;
} elseif ( 'mf2_' === substr( $field, 0, 4 ) ) {
$mf2['properties'][ substr( $field, 4 ) ] = $val;
}
}

// Time Information
$published = micropub_get_post_datetime( $post );
$updated = micropub_get_post_datetime( $post, 'modified' );
$mf2['properties']['published'] = array( $published->format( DATE_W3C ) );
if ( $published->getTimestamp() !== $updated->getTimestamp() ) {
$mf2['properties']['updated'] = array( $updated->format( DATE_W3C ) );
}

if ( ! empty( $post->post_title ) ) {
$mf2['properties']['name'] = array( $post->post_title );
}

if ( ! empty( $post->post_excerpt ) ) {
$mf2['properties']['summary'] = array( htmlspecialchars_decode( $post->post_excerpt ) );
}
if ( ! array_key_exists( 'content', $mf2['properties'] ) && ! empty( $post->post_content ) ) {
$mf2['properties']['content'] = array( htmlspecialchars_decode( $post->post_content ) );
}

return $mf2;
}
}
51 changes: 8 additions & 43 deletions includes/class-micropub-endpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ public static function init() {
add_filter( 'rest_request_after_callbacks', array( static::class, 'return_micropub_error' ), 10, 3 );
}

public static function get( $array, $key, $default = array() ) {
if ( is_array( $array ) ) {
return isset( $array[ $key ] ) ? $array[ $key ] : $default;
public static function get( $a, $key, $d = array() ) {
if ( is_array( $a ) ) {
return isset( $a[ $key ] ) ? $a[ $key ] : $d;
}
return $default;
return $d;
}

public static function register_route() {
Expand Down Expand Up @@ -986,8 +986,9 @@ public static function parse_geo_uri( $uri ) {
public static function store_micropub_auth_response( $args ) {
$micropub_auth_response = static::$micropub_auth_response;
if ( $micropub_auth_response || ( is_assoc_array( $micropub_auth_response ) ) ) {
$args['meta_input'] = mp_get( $args, 'meta_input' );
$args['meta_input']['micropub_auth_response'] = wp_array_slice_assoc( $micropub_auth_response, array( 'client_id', 'client_name', 'client_icon', 'uuid' ) );
$args['meta_input'] = mp_get( $args, 'meta_input' );
$args['meta_input']['micropub_auth_response'] = wp_array_slice_assoc( $micropub_auth_response, array( 'client_id', 'client_name', 'client_icon', 'uuid' ) );
$args['meta_input']['micropub_version']['version'] = micropub_get_plugin_version();
}
return $args;
}
Expand All @@ -1003,8 +1004,7 @@ public static function store_micropub_auth_response( $args ) {
*/
public static function store_mf2( $args ) {
// Properties that map to WordPress properties.
// TODO: We need to still store content because the plugin adds markup to the content stored.
$excludes = array( 'name', 'published', 'updated', 'summary', 'updated' );
$excludes = array( 'name', 'published', 'updated', 'summary', 'updated', 'content', 'visibility' );
pfefferle marked this conversation as resolved.
Show resolved Hide resolved
$props = mp_get( static::$input, 'properties', false );
if ( ! isset( $args['ID'] ) && $props ) {
$args['meta_input'] = mp_get( $args, 'meta_input' );
Expand Down Expand Up @@ -1073,41 +1073,6 @@ public static function store_mf2( $args ) {
return $args;
}

/**
* Returns the mf2 properties for a post.
*/
public static function get_mf2( $post_id ) {
$mf2 = array();
$post = get_post( $post_id );

foreach ( get_post_meta( $post_id ) as $field => $val ) {
$val = maybe_unserialize( $val[0] );
if ( 'mf2_type' === $field ) {
$mf2['type'] = $val;
} elseif ( 'mf2_' === substr( $field, 0, 4 ) ) {
$mf2['properties'][ substr( $field, 4 ) ] = $val;
}
}

// Time Information
$published = micropub_get_post_datetime( $post );
$updated = micropub_get_post_datetime( $post, 'modified' );
$mf2['properties']['published'] = array( $published->format( DATE_W3C ) );
if ( $published->getTimestamp() !== $updated->getTimestamp() ) {
$mf2['properties']['updated'] = array( $updated->format( DATE_W3C ) );
}

if ( ! empty( $post->post_title ) ) {
$mf2['properties']['name'] = array( $post->post_title );
}

if ( ! empty( $post->post_excerpt ) ) {
$mf2['properties']['summary'] = array( $post->post_excerpt );
}

return $mf2;
}

/* Takes form encoded input and converts to json encoded input */
public static function form_to_json( $data ) {
$input = array();
Expand Down
4 changes: 2 additions & 2 deletions includes/class-micropub-error.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ public function __construct( $error, $error_description, $code = 200, $debug = n
}
}

public function set_debug( $array ) {
public function set_debug( $a ) {
$data = $this->get_data();
$this->set_data( array_merge( $data, $array ) );
$this->set_data( array_merge( $data, $a ) );
}

public function to_wp_error() {
Expand Down
44 changes: 41 additions & 3 deletions includes/class-micropub-render.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,50 @@
<?php

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

/* Generate Post Content and Save it for Pre 2.4.0 versions
* add_filter( 'micropub_post_content', array( 'Micropub_Render', 'generate_post_content' ), 1, 2 );
*/

add_filter( 'the_content', array( 'Micropub_Render', 'render_content' ), 1 );

/**
* Micropub Render Class
*/
class Micropub_Render {
/**
* Dynamically Renders Microformats 2
*
*/
public static function render_content( $content ) {
// If this is not a micropub post return without any further work.
if ( ! is_micropub_post() ) {
return $content;
}

if ( self::should_dynamic_render() ) {
$input = Micropub_Base::get_mf2( get_the_ID() );
return self::generate_post_content( $content, $input );
}

return $content;
}

public static function should_dynamic_render( $post = null ) {
$post = get_post();
if ( class_exists( 'Post_Kinds_Plugin' ) ) {
$should = false;
} else {
$version = get_post_meta( $post->ID, 'micropub_version', true );
if ( ! $version ) {
$should = false;
} elseif ( get_post_meta( $post->ID, 'mf2_content', true ) ) {
$should = false;
} else {
$should = true;
}
}
return apply_filters( 'micropub_dynamic_render', $should, $post );
}

/**
* Generates and returns a post_content string suitable for wp_insert_post()
Expand Down Expand Up @@ -70,8 +107,9 @@ public static function generate_post_content( $post_content, $input ) {
}
}

$checkin = isset( $props['checkin'] ) ? $props['checkin'][0] : null;
$checkin = isset( $props['checkin'] );
if ( $checkin ) {
$checkin = wp_is_numeric_array( $props['checkin'] ) ? $props['checkin'][0] : $props['checkin'];
$name = $checkin['properties']['name'][0];
$urls = $checkin['properties']['url'];
$lines[] = '<p>Checked into <a class="h-card p-location" href="' .
Expand Down
6 changes: 3 additions & 3 deletions includes/compat-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,10 @@ function array_key_first( array $arr ) {

// Polyfill for pre-PHP 7.3.
if ( ! function_exists( 'array_key_last' ) ) {
function array_key_last( $array ) {
if ( ! is_array( $array ) || empty( $array ) ) {
function array_key_last( $a ) {
if ( ! is_array( $a ) || empty( $a ) ) {
return null;
}
return array_keys( $array )[ count( $array ) - 1 ];
return array_keys( $a )[ count( $a ) - 1 ];
}
}
96 changes: 60 additions & 36 deletions includes/functions.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

function is_assoc_array( $array ) {
return is_array( $array ) && array_values( $array ) !== $array;
function is_assoc_array( $assoc ) {
return is_array( $assoc ) && array_values( $assoc ) !== $assoc;
}


Expand All @@ -23,10 +23,10 @@ function getallheaders() {
}

if ( ! function_exists( 'mp_get' ) ) {
function mp_get( $array, $key, $default = array(), $index = false ) {
$return = $default;
if ( is_array( $array ) && isset( $array[ $key ] ) ) {
$return = $array[ $key ];
function mp_get( $data, $key, $def = array(), $index = false ) {
$return = $def;
if ( is_array( $data ) && isset( $data[ $key ] ) ) {
$return = $data[ $key ];
}
if ( $index && wp_is_numeric_array( $return ) && ! empty( $return ) ) {
$return = $return[0];
Expand All @@ -37,10 +37,10 @@ function mp_get( $array, $key, $default = array(), $index = false ) {

if ( ! function_exists( 'mp_filter' ) ) {
// Searches for partial matches in an array of strings
function mp_filter( $array, $filter ) {
function mp_filter( $a, $filter ) {
return array_values(
array_filter(
$array,
$a,
function ( $value ) use ( $filter ) {
return ( false !== stripos( $value, $filter ) );
}
Expand All @@ -55,6 +55,24 @@ function micropub_get_response() {
}
}

if ( ! function_exists( 'is_micropub_post' ) ) {
function is_micropub_post( $post = null ) {
$post = get_post( $post );
if ( ! $post ) {
return false;
}
$response = get_post_meta( $post->ID, 'micropub_version', true );
if ( $response ) {
return true;
}
$response = get_post_meta( $post->ID, 'micropub_auth_response', true );
if ( ! $response ) {
return false;
}
return true;
}
}

if ( ! function_exists( 'micropub_get_client_info' ) ) {
function micropub_get_client_info( $post = null ) {
$post = get_post( $post );
Expand Down Expand Up @@ -160,41 +178,47 @@ function micropub_get_post_datetime( $post = null, $field = 'date', $timezone =
}
}

function get_micropub_error( $obj ) {
if ( is_array( $obj ) ) {
// When checking the result of wp_remote_post
if ( isset( $obj['body'] ) ) {
$body = json_decode( $obj['body'], true );
if ( isset( $body['error'] ) ) {
return new WP_Micropub_Error(
$body['error'],
isset( $body['error_description'] ) ? $body['error_description'] : null,
$obj['response']['code']
);
if ( ! function_exists( 'get_micropub_error' ) ) {
function get_micropub_error( $obj ) {
if ( is_array( $obj ) ) {
// When checking the result of wp_remote_post
if ( isset( $obj['body'] ) ) {
$body = json_decode( $obj['body'], true );
if ( isset( $body['error'] ) ) {
return new WP_Micropub_Error(
$body['error'],
isset( $body['error_description'] ) ? $body['error_description'] : null,
$obj['response']['code']
);
}
}
} elseif ( is_object( $obj ) && 'WP_Micropub_Error' === get_class( $obj ) ) {
$data = $obj->get_data();
if ( isset( $data['error'] ) ) {
return $obj;
}
}
} elseif ( is_object( $obj ) && 'WP_Micropub_Error' === get_class( $obj ) ) {
$data = $obj->get_data();
if ( isset( $data['error'] ) ) {
return $obj;
}
return false;
}
return false;
}

function is_micropub_error( $obj ) {
return ( $obj instanceof WP_Micropub_Error );
if ( ! function_exists( 'is_micropub_error' ) ) {
function is_micropub_error( $obj ) {
return ( $obj instanceof WP_Micropub_Error );
}
}

// Converts WP_Error into Micropub Error
function micropub_wp_error( $error ) {
if ( is_wp_error( $error ) ) {
$data = $error->get_error_data();
$status = isset( $data['status'] ) ? $data['status'] : 200;
if ( is_array( $data ) ) {
unset( $data['status'] );
if ( ! function_exists( 'micropub_wp_error' ) ) {
// Converts WP_Error into Micropub Error
function micropub_wp_error( $error ) {
if ( is_wp_error( $error ) ) {
$data = $error->get_error_data();
$status = isset( $data['status'] ) ? $data['status'] : 200;
if ( is_array( $data ) ) {
unset( $data['status'] );
}
return new WP_Micropub_Error( $error->get_error_code(), $error->get_error_message(), $status, $data );
}
return new WP_Micropub_Error( $error->get_error_code(), $error->get_error_message(), $status, $data );
return null;
}
return null;
}
Loading
Loading