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
8 changes: 6 additions & 2 deletions src/Admin/Admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,8 @@ public function handle_save_source(): void {
$categories = $this->infer_categories_from_feed_url( $url );
}

$post_type = sanitize_key( wp_unslash( $_POST['post_type'] ?? '' ) );

$settings = [
'interval_minutes' => $interval,
'categories' => $categories,
Expand All @@ -413,11 +415,14 @@ public function handle_save_source(): void {
'include_keywords' => $include,
'exclude_keywords' => $exclude,
'article_length' => $length,
'post_type' => $post_type,
];

if ( sanitize_key( wp_unslash( $_POST['source_type'] ?? 'rss' ) ) === 'scrape' ) {
$settings = array_merge( $settings, $this->scrape_settings_from_post() );
\AggregateIt\Source\ScraperPostTypes::remember( (string) $settings['post_type'] );
}
if ( $post_type !== '' ) {
\AggregateIt\Source\ScraperPostTypes::remember( $post_type );
}

$repo = $this->plugin->sources();
Expand Down Expand Up @@ -490,7 +495,6 @@ private function scrape_settings_from_post(): array {

return [
'source_type' => 'scrape',
'post_type' => sanitize_key( wp_unslash( $_POST['post_type'] ?? '' ) ),
'processing' => in_array( $processing, [ 'passthrough', 'rewrite' ], true ) ? $processing : 'passthrough',
'scrape' => [
'discovery' => [
Expand Down
2 changes: 1 addition & 1 deletion src/Admin/views/sources.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@
value="<?php echo esc_attr( $editing->url ?? '' ); ?>" placeholder="https://example.com/feed"></td>
</tr>

<tr class="ai-scrape-row">
<tr>
<th><label for="ai-post-type"><?php esc_html_e( 'Save items as (post type)', 'aggregate-it' ); ?></label></th>
<td>
<input name="post_type" id="ai-post-type" type="text" class="regular-text" list="ai-post-types"
Expand Down
3 changes: 2 additions & 1 deletion src/Maintenance/GlobalRulesRefresher.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace AggregateIt\Maintenance;

use AggregateIt\Publish\Meta;
use AggregateIt\Publish\Rules;
use AggregateIt\Settings;
use AggregateIt\Source\SourceRepository;
Expand Down Expand Up @@ -73,7 +74,7 @@ public function apply_type( string $type, array $rules, int $now ): void {
if ( isset( $owned[ $key ] ) ) {
continue;
}
update_post_meta( (int) $id, $key, $value );
Meta::write( (int) $id, $key, $value );
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/Maintenance/RulesRefresher.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace AggregateIt\Maintenance;

use AggregateIt\Publish\Meta;
use AggregateIt\Publish\Rules;
use AggregateIt\Source\SourceRepository;
use AggregateIt\Support\Json;
Expand Down Expand Up @@ -62,7 +63,7 @@ public function run(): void {
}

foreach ( Rules::apply( $values, $rules[ $sid ], $now ) as $key => $value ) {
update_post_meta( $id, sanitize_key( (string) $key ), $value );
Meta::write( (int) $id, sanitize_key( (string) $key ), $value );
}
}

Expand Down
21 changes: 21 additions & 0 deletions src/Publish/Meta.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace AggregateIt\Publish;

defined( 'ABSPATH' ) || exit;

/**
* Writes a derived/mapped value to a post field. When ACF is active it uses update_field so
* the value lands in the matching ACF field with its field-key reference (so the ACF editor
* shows it and date/select fields format correctly); otherwise it falls back to post meta.
*/
final class Meta {

public static function write( int $post_id, string $key, $value ): void {
if ( function_exists( 'update_field' ) ) {
update_field( $key, $value, $post_id );
return;
}
update_post_meta( $post_id, $key, $value );
}
}
17 changes: 9 additions & 8 deletions src/Publish/PostFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,13 @@ public function create( int $cluster_id, Item $item, array $structured, string $
$slug = (string) ( $structured['slug'] ?? '' );
$slug = $this->slugs->generate( $slug !== '' ? $slug : $keyword, $title );

$source = $item->source_id ? $this->sources->get( $item->source_id ) : null;
$status = $source ? $source->publish_status( $this->settings->publish_status() ) : $this->settings->publish_status();
$source = $item->source_id ? $this->sources->get( $item->source_id ) : null;
$status = $source ? $source->publish_status( $this->settings->publish_status() ) : $this->settings->publish_status();
$post_type = $source && $source->post_type_connection() !== '' ? $source->post_type_connection() : $this->settings->target_post_type();

$post_id = wp_insert_post(
[
'post_type' => $this->settings->target_post_type(),
'post_type' => $post_type,
'post_status' => $status,
'post_title' => $title,
'post_name' => $slug,
Expand All @@ -53,7 +54,7 @@ public function create( int $cluster_id, Item $item, array $structured, string $
update_post_meta( $post_id, '_ai_schema_type', $item->flags['schema_type'] ?? 'Article' );
update_post_meta( $post_id, '_ai_prompt_version', AGGREGATE_IT_VERSION );

$this->assign_terms( (int) $post_id, $item->source_id, (string) ( $structured['category'] ?? '' ) );
$this->assign_terms( (int) $post_id, $post_type, $item->source_id, (string) ( $structured['category'] ?? '' ) );

return (int) $post_id;
}
Expand All @@ -62,9 +63,9 @@ public function create( int $cluster_id, Item $item, array $structured, string $
* Categorize by the AI's per-article topic (created on first use), falling back to the
* feed's configured categories when the AI gives nothing. Tags come from the feed.
*/
private function assign_terms( int $post_id, int $source_id, string $ai_category ): void {
private function assign_terms( int $post_id, string $post_type, int $source_id, string $ai_category ): void {
$source = $source_id ? $this->sources->get( $source_id ) : null;
$taxes = get_object_taxonomies( $this->settings->target_post_type() );
$taxes = get_object_taxonomies( $post_type );

if ( in_array( 'category', $taxes, true ) ) {
$ids = [];
Expand Down Expand Up @@ -129,7 +130,7 @@ public function create_mapped( Item $item ): int {
update_post_meta( $post_id, '_ai_prompt_version', AGGREGATE_IT_VERSION );

foreach ( $mapped['meta'] as $key => $value ) {
update_post_meta( $post_id, sanitize_key( (string) $key ), $value );
Meta::write( $post_id, sanitize_key( (string) $key ), $value );
}
foreach ( $mapped['terms'] as $taxonomy => $names ) {
if ( taxonomy_exists( (string) $taxonomy ) ) {
Expand All @@ -140,7 +141,7 @@ public function create_mapped( Item $item ): int {
$rules = $source ? $source->rules() : [];
if ( $rules ) {
foreach ( Rules::apply( $values, $rules, time() ) as $key => $value ) {
update_post_meta( $post_id, sanitize_key( (string) $key ), $value );
Meta::write( $post_id, sanitize_key( (string) $key ), $value );
}
update_post_meta( $post_id, '_ai_rule_values', wp_json_encode( $values ) );
update_post_meta( $post_id, '_ai_source_id', (int) $item->source_id );
Expand Down
Loading