Skip to content

Commit 17d1525

Browse files
committed
feat: Added ability to set local category
1 parent d031ea8 commit 17d1525

5 files changed

+53
-5
lines changed

auto-copy-posts-for-wordpress.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
/**
44
* Plugin Name: Auto Copy Posts for WordPress
55
* Description: Sync posts from one WordPress site to another
6-
* Version: 1.7.0
6+
* Version: 1.8.0
77
* Requires PHP: 8.0
88
* Author: Nick Stewart
99
* Author URI: https://nickstewart.me

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "auto-copy-posts-for-wordpress",
3-
"version": "1.7.0",
3+
"version": "1.8.0",
44
"description": "![banner](https://raw.githubusercontent.com/nickstewart95/auto-copy-posts-for-wordpress/main/banner-772x250.png)",
55
"js": "src/resources/build/build.js",
66
"css": "src/resources/build/build.css",

readme.txt

+6-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Author URI: https://nickstewart.me
44
Tags: sync, copy, posts
55
Requires at least: 5.3
66
Tested up to: 6.1.1
7-
Stable tag: 1.7.0
7+
Stable tag: 1.8.0
88
Requires PHP: 8.0
99

1010
Sync posts from one Wordpress site to another
@@ -15,6 +15,11 @@ A simple WordPress plugin that can sync posts from one WordPress site to another
1515

1616
== Changelog ==
1717

18+
= 1.8 =
19+
20+
- Added ability to set local category to sync to
21+
- Fixed bug when grabbing featured image field
22+
1823
= 1.7 =
1924

2025
- Added additional logging

src/AutoCopy.php

+36-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
use Carbon\Carbon;
88
use Jenssegers\Blade\Blade;
99

10-
define('AUTO_COPY_POSTS_VERSION', '1.7.0');
10+
define('AUTO_COPY_POSTS_VERSION', '1.8.0');
1111
define('AUTO_COPY_POSTS_FILE', __FILE__);
1212

1313
class AutoCopy {
@@ -306,6 +306,13 @@ public static function plugin_setting_post_type_fields(): array {
306306
'Plural name of the local post type being synced',
307307
'value' => self::DEFAULT_POST_TYPE_PLURAL,
308308
],
309+
[
310+
'name' => 'auto_copy_posts_custom_post_type_category_1',
311+
'type' => 'text',
312+
'title' => 'Post Type Local Category - 1',
313+
'description' => 'Local category post gets placed in',
314+
'value' => '',
315+
],
309316
[
310317
'name' => 'auto_copy_posts_custom_post_type_content_field_1',
311318
'type' => 'text',
@@ -1159,7 +1166,7 @@ public static function findFeaturedImageFieldByPostType(
11591166
): string|bool {
11601167
global $wpdb;
11611168
$plural_query = $wpdb->get_results(
1162-
"SELECT * FROM {$wpdb->prefix}options WHERE option_name LIKE 'auto_copy_posts_custom_post_type_single_name_1%' AND option_value ='{$external_post_type_single}' LIMIT 1",
1169+
"SELECT * FROM {$wpdb->prefix}options WHERE option_name LIKE 'auto_copy_posts_custom_post_type_single_name_%' AND option_value ='{$external_post_type_single}' LIMIT 1",
11631170
);
11641171

11651172
if (empty($plural_query)) {
@@ -1178,6 +1185,33 @@ public static function findFeaturedImageFieldByPostType(
11781185
return reset($local_post_type)->option_value;
11791186
}
11801187

1188+
/**
1189+
* Returns the category by post type
1190+
*/
1191+
public static function findCategoryFieldByPostType(
1192+
string $external_post_type_single
1193+
): string|bool {
1194+
global $wpdb;
1195+
$plural_query = $wpdb->get_results(
1196+
"SELECT * FROM {$wpdb->prefix}options WHERE option_name LIKE 'auto_copy_posts_custom_post_type_single_name_%' AND option_value ='{$external_post_type_single}' LIMIT 1",
1197+
);
1198+
1199+
if (empty($plural_query)) {
1200+
return false;
1201+
}
1202+
1203+
$id = substr(reset($plural_query)->option_name, -1);
1204+
$local_post_type = $wpdb->get_results(
1205+
"SELECT * FROM {$wpdb->prefix}options WHERE option_name = 'auto_copy_posts_custom_post_type_category_{$id}' LIMIT 1",
1206+
);
1207+
1208+
if (empty($local_post_type)) {
1209+
return false;
1210+
}
1211+
1212+
return reset($local_post_type)->option_value;
1213+
}
1214+
11811215
/**
11821216
* Confirms if the request is coming from the settings page
11831217
*/

src/Events.php

+9
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,15 @@ public static function createPost(string $transient): void {
293293
}
294294
}
295295

296+
// Add to local category is specified
297+
$local_category = AutoCopy::findCategoryFieldByPostType($post['type']);
298+
if (!empty($local_category)) {
299+
$category_ids[] = self::createOrFindTerm(
300+
'category',
301+
$local_category,
302+
);
303+
}
304+
296305
// Grab where the content field lives, either as content or in ACF
297306
$content_field = AutoCopy::findContentFieldByPostType($post['type']);
298307
if (!empty($content_field)) {

0 commit comments

Comments
 (0)