Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
29 changes: 19 additions & 10 deletions src/plugin/class-subjects-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@

namespace DotOrg\TryWordPress;

use DateTime;
use DateTimeZone;
use Exception;
use WP_Error;
use WP_REST_Controller;
use WP_REST_Response;
Expand Down Expand Up @@ -290,11 +289,15 @@ public function create_item( $request ): WP_REST_Response|WP_Error {
}

$subject_type = $this->get_subject_type( $request );
update_post_meta( $item['ID'], 'subject_type', $subject_type );
update_post_meta( $item['ID'], 'subject_type', $subject_type->value );

do_action( 'dl_data_saved', Subject::from_post( $item['ID'] ), 'create' );
try {
TransformersRegistry::handle( $subject_type, Subject::from_post( $item['ID'] ) );
} catch ( Exception $e ) {
return new WP_Error( $e->getCode(), $e->getMessage() );
}

return $this->prepare_item_for_response( $item, $request, $subject_type );
return $this->prepare_item_for_response( $item, $request );
}

public function update_item( $request ): WP_REST_Response|WP_Error {
Expand All @@ -316,7 +319,13 @@ public function update_item( $request ): WP_REST_Response|WP_Error {
update_post_meta( $item['ID'], $key, $value );
}

do_action( 'dl_data_saved', Subject::from_post( $item['ID'] ), 'update' );
$subject_type = $this->get_subject_type( $request );

try {
TransformersRegistry::handle( $subject_type, Subject::from_post( $item['ID'] ) );
} catch ( Exception $e ) {
return new WP_Error( $e->getCode(), $e->getMessage() );
}

return $this->prepare_item_for_response( $item, $request );
}
Expand Down Expand Up @@ -385,7 +394,7 @@ public function prepare_item_for_response( $item, $request ): WP_REST_Response|W
'transformedId' => absint( get_post_meta( $item['ID'], Transformer::META_KEY_LIBERATED_OUTPUT, true ) ),
);

foreach ( array_keys( Schema::get()[ $subject_type ]['fields'] ) as $field_name ) {
foreach ( array_keys( Schema::get()[ $subject_type->value ]['fields'] ) as $field_name ) {
$response[ 'raw' . ucfirst( $field_name ) ] = get_post_meta( $item['ID'], 'raw_' . $field_name, true );
$response[ 'parsed' . ucfirst( $field_name ) ] = get_post_meta( $item['ID'], 'parsed_' . $field_name, true );
}
Expand All @@ -409,7 +418,7 @@ public function prepare_item_for_database( $request ): array {
$prepared_post['post_author'] = $request_data['authorId'] ?? '';

$prepared_post['meta'] = array();
foreach ( array_keys( Schema::get()[ $subject_type ]['fields'] ) as $field_name ) {
foreach ( array_keys( Schema::get()[ $subject_type->value ]['fields'] ) as $field_name ) {
$prepared_post['meta'][ 'raw_' . $field_name ] = $request_data[ 'raw' . ucfirst( $field_name ) ] ?? '';
$prepared_post['meta'][ 'parsed_' . $field_name ] = $request_data[ 'parsed' . ucfirst( $field_name ) ] ?? '';
}
Expand Down Expand Up @@ -452,8 +461,8 @@ public function get_post_id_by_guid( string $guid ): ?int {
return null;
}

private function get_subject_type( $request ): string {
private function get_subject_type( $request ): SubjectType {
preg_match( '/\/subjects\/([^\/]+)/', $request->get_route(), $matches );
return $matches[1];
return SubjectType::tryFrom( $matches[1] );
}
}
88 changes: 42 additions & 46 deletions src/plugin/class-transformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,85 +2,81 @@

namespace DotOrg\TryWordPress;

use WP_Post;

class Transformer {
public const string META_KEY_LIBERATED_SOURCE = '_data_liberation_source';
public const string META_KEY_LIBERATED_OUTPUT = '_data_liberation_output';

public function __construct() {
add_action( 'dl_data_saved', array( $this, 'transform' ), 10, 2 );
}

private function get_post_type_for_transformed_post( int|WP_Post $liberated_post ): string {
if ( is_int( $liberated_post ) ) {
$liberated_post = get_post( $liberated_post );
}

$subject_type = get_post_meta( $liberated_post->ID, 'subject_type', true );
switch ( $subject_type ) {
case 'blog-post':
$post_type = 'post';
break;
case 'product':
$post_type = 'product';
break;
case 'page':
$post_type = 'page';
break;
default:
$post_type = 'post';
}
TransformersRegistry::add(
SubjectType::BLOGPOST,
array(
'slug' => 'try_wordpress',
'description' => 'Try WordPress handling blog-post natively',
),
array(
$this,
'handler',
)
);

// @TODO: filter name would be changed w.r.t new verb in place of 'transformed' once its decided
return apply_filters( 'post_type_for_transformed_post', $post_type, $liberated_post );
TransformersRegistry::add(
SubjectType::PAGE,
array(
'slug' => 'try_wordpress',
'description' => 'Try WordPress handling page natively',
),
array(
$this,
'handler',
)
);
}

public function transform( Subject $subject, string $verb ): bool {
if ( apply_filters( 'skip_native_transformation', false ) ) {
return true;
}

$liberated_post_id = $subject->id();
$liberated_post = get_post( $liberated_post_id );

$transformed_post_id = get_post_meta( $liberated_post->ID, self::META_KEY_LIBERATED_OUTPUT, true );
public function get_post_type( Subject $subject ): string {
return match ( $subject->type ) {
'page' => 'page',
default => 'post',
};
}

$title = get_post_meta( $liberated_post->ID, 'parsed_title', true );
public function handler( Subject $subject ) {
// Since parsed versions come from paste_handler in frontend, look for them in postmeta, instead of subject instance
$title = get_post_meta( $subject->id(), 'parsed_title', true );
if ( empty( $title ) ) {
$title = '[Title]';
}
$body = get_post_meta( $liberated_post->ID, 'parsed_content', true );
$body = get_post_meta( $subject->id(), 'parsed_content', true );
if ( empty( $body ) ) {
$body = '[Body]';
}

$args = array(
'post_author' => $liberated_post->post_author,
'post_date' => get_post_meta( $liberated_post->ID, 'parsed_date', true ),
'post_author' => $subject->author_id(),
'post_date' => get_post_meta( $subject->id(), 'parsed_date', true ),
'post_content' => $body,
'post_title' => $title,
'post_status' => 'publish',
'post_type' => $this->get_post_type_for_transformed_post( $liberated_post->ID ),
'post_type' => $this->get_post_type( $subject ),
);

// have we already transformed this subject before?
$transformed_post_id = get_post_meta( $subject->id(), self::META_KEY_LIBERATED_OUTPUT, true );
if ( ! empty( $transformed_post_id ) ) {
$args['ID'] = $transformed_post_id;
}

add_filter( 'wp_insert_post_empty_content', '__return_false' );
$inserted_post_id = wp_insert_post( $args, true );
$inserted_post_id = wp_insert_post( $args );
remove_filter( 'wp_insert_post_empty_content', '__return_false' );

// @TODO: handle attachments, terms etc in future
// Note: Do not need anything from postmeta.
// We should potentially use another plugin here for this purpose and call its API to do it for us.

if ( 0 === $inserted_post_id || is_wp_error( $inserted_post_id ) ) {
return false;
if ( 0 === $inserted_post_id ) {
return null;
}

update_post_meta( $inserted_post_id, self::META_KEY_LIBERATED_SOURCE, $liberated_post->ID );
update_post_meta( $liberated_post->ID, self::META_KEY_LIBERATED_OUTPUT, $inserted_post_id );
return true;
return $inserted_post_id;
}
}
4 changes: 2 additions & 2 deletions src/plugin/class-transformersregistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public static function add( SubjectType $type, array $identifier, callable $hand

self::$handlers[ $type->value ][ $identifier['slug'] ] = array(
'slug' => $identifier['slug'],
'description' => $identifier['desc'],
'description' => $identifier['description'],
'handler' => $handler,
);
}
Expand Down Expand Up @@ -85,8 +85,8 @@ public static function handle( SubjectType $type, Subject $subject ): void {
$transformed_post_id = $chosen['handler']( $subject );

if ( $transformed_post_id ) {
update_post_meta( $subject->id(), Transformer::META_KEY_LIBERATED_OUTPUT, $transformed_post_id );
update_post_meta( $transformed_post_id, Transformer::META_KEY_LIBERATED_SOURCE, $subject->id() );
update_post_meta( $subject->id(), Transformer::META_KEY_LIBERATED_OUTPUT, $transformed_post_id );
}
}

Expand Down
65 changes: 0 additions & 65 deletions tests/plugin/test-transformer.php

This file was deleted.

Loading