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
25 changes: 10 additions & 15 deletions posts-bridge/addons/airtable/hooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,22 +72,17 @@ function posts_bridge_airtable_register_http_defaults( $setting ) {
$setting['backends'] = $backends;
}

$schemas = array_column( $credentials, 'schema' );
$exists = array_search( $credential_schema, $schemas, true );

if ( false === $exists ) {
$names = array_column( $credentials, 'name' );

if ( ! in_array( $credential_name, $names, true ) ) {
$credentials[] = array(
'name' => $credential_name,
'schema' => $credential_schema,
'access_token' => 'your-api-key',
'expires_at' => time() + 60 * 60 * 24 * 365 * 100,
);
$names = array_column( $credentials, 'name' );

if ( ! in_array( $credential_name, $names, true ) ) {
$credentials[] = array(
'name' => $credential_name,
'schema' => $credential_schema,
'access_token' => 'your-api-key',
'expires_at' => time() + 60 * 60 * 24 * 365 * 100,
);

$setting['credentials'] = $credentials;
}
$setting['credentials'] = $credentials;
}
}

Expand Down
29 changes: 12 additions & 17 deletions posts-bridge/addons/grist/hooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ function ( $schema, $addon ) {
}

$schema['properties']['foreign_key']['const'] = 'id';
$schema['properties']['endpoint']['default'] = '/v0/{baseId}/{tableName}/records';
$schema['properties']['single_endpoint']['const'] = '/v0/{baseId}/{tableName}/records';
$schema['properties']['endpoint']['default'] = '/api/docs/{docId}/tables/{tableId}/records';
$schema['properties']['single_endpoint']['const'] = '/api/docs/{docId}/tables/{tableId}/records';
$schema['properties']['backend']['default'] = 'Grist API';
$schema['properties']['method']['const'] = 'GET';

Expand Down Expand Up @@ -54,7 +54,7 @@ function posts_bridge_grist_register_http_defaults( $setting ) {
$exists = array_search( $backend_url, $urls, true );

if ( false === $exists ) {
$names = array_column( $backends, 'base_url' );
$names = array_column( $backends, 'name' );

if ( ! in_array( $backend_name, $names, true ) ) {
$backends[] = array(
Expand All @@ -72,22 +72,17 @@ function posts_bridge_grist_register_http_defaults( $setting ) {
$setting['backends'] = $backends;
}

$schemas = array_column( $credentials, 'schema' );
$exists = array_search( $credential_schema, $schemas, true );
$names = array_column( $credentials, 'name' );

if ( false === $exists ) {
$names = array_column( $credentials, 'name' );

if ( ! in_array( $credential_name, $names, true ) ) {
$credentials[] = array(
'name' => $credential_name,
'schema' => $credential_schema,
'access_token' => 'your-api-key',
'expires_at' => time() + 60 * 60 * 24 * 365 * 100,
);
if ( ! in_array( $credential_name, $names, true ) ) {
$credentials[] = array(
'name' => $credential_name,
'schema' => $credential_schema,
'access_token' => 'your-api-key',
'expires_at' => time() + 60 * 60 * 24 * 365 * 100,
);

$setting['credentials'] = $credentials;
}
$setting['credentials'] = $credentials;
}
}

Expand Down
1 change: 0 additions & 1 deletion posts-bridge/includes/class-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
*/

use POSTS_BRIDGE\Settings_Store;
use POSTS_BRIDGE\Posts_Bridge;
use POSTS_BRIDGE\Addon;
use POSTS_BRIDGE\Post_Bridge;
use HTTP_BRIDGE\Backend;
Expand Down
133 changes: 98 additions & 35 deletions posts-bridge/includes/class-custom-post-type.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,21 @@ class Custom_Post_Type {
'wp_navigation',
'wp_font_family',
'wp_font_face',
/* ACF */
'acf-field',
'acf-field-group',
'acf-post-type',
'acf-taxonomy',
/* WPForms */
'wpforms',
'wpforms-template',
/* Contact Form 7 */
'wpcf7_contact_form',
/* Forminator */
'frm_form_actions',
'frm_styles',
/* Ninja forms */
'nf_sub',
);

/**
Expand Down Expand Up @@ -407,7 +422,9 @@ private static function registry() {
return self::$registry;
}

self::$registry = (array) get_option( self::OPTION_NAME, array() );
$post_types = (array) get_option( self::OPTION_NAME, array() );
self::$registry = $post_types;

return self::$registry;
}

Expand Down Expand Up @@ -436,14 +453,14 @@ private static function init() {
public static function post_types() {
$post_types = array_keys( get_post_types() );

return array_values(
array_filter(
$post_types,
static function ( $post_type ) {
return ! in_array( $post_type, self::INTERNAL_TYPES, true );
}
)
);
$public_post_types = array();
foreach ( $post_types as $pt ) {
if ( ! in_array( $pt, self::INTERNAL_TYPES, true ) ) {
$public_post_types[] = $pt;
}
}

return $public_post_types;
}

/**
Expand All @@ -452,7 +469,7 @@ static function ( $post_type ) {
* @param string $name Type name.
* @param array $args Registration arguments.
*
* @return WP_Post_Type|WP_Error
* @return \WP_Post_Type|\WP_Error
*/
private static function register_post_type( $name, $args ) {
$args['labels'] = array(
Expand Down Expand Up @@ -504,6 +521,7 @@ static function ( $store ) {
'general',
static function ( $data ) {
$data['post_types'] = self::post_types();

return $data;
}
);
Expand All @@ -514,6 +532,7 @@ static function ( $data ) {
if ( isset( $data['post_types'] ) ) {
unset( $data['post_types'] );
}

return $data;
},
9
Expand All @@ -522,6 +541,50 @@ static function ( $data ) {
);
}

private static function get_acf_cpts( $filter = null ) {
if ( ! function_exists( 'acf_get_raw_post_types' ) ) {
return array();
}

$acf_raw_cpts = acf_get_raw_post_types();

$acf_cpts = array();
foreach ( $acf_raw_cpts as $acf_raw_cpt ) {
$name = $acf_raw_cpt['post_type'];

if ( is_array( $filter ) && ! in_array( $name, $filter, true ) ) {
continue;
}

$acf_cpts[] = array(
'name' => $name,
'label' => $acf_raw_cpt['labels']['name'],
'singular_label' => $acf_raw_cpt['labels']['singular_label'],
'description' => $acf_raw_cpt['description'],
'public' => $acf_raw_cpt['public'],
'exclude_from_search' => $acf_raw_cpt['exclude_from_search'],
'publicly_queryable' => $acf_raw_cpt['publicly_queryable'],
'show_ui' => $acf_raw_cpt['show_ui'],
'show_in_menu' => $acf_raw_cpt['show_in_menu'],
'show_in_nav_menus' => $acf_raw_cpt['show_in_nav_menus'],
'show_in_rest' => $acf_raw_cpt['show_in_rest'],
'rest_base' => $acf_raw_cpt['rest_base'],
'menu_position' => $acf_raw_cpt['menu_position'],
'capability_type' => $acf_raw_cpt['singular_capability_name'],
'map_meta_cap' => true,
'supports' => $acf_raw_cpt['supports'],
'taxonomies' => $acf_raw_cpt['taxonomies'],
'has_archive' => $acf_raw_cpt['has_archive'],
'rewrite' => $acf_raw_cpt['rewrite']['permalink_rewrite'] ?? false,
'query_var' => $acf_raw_cpt['query_var'],
'meta' => array(),
'_acf' => true,
);
}

return $acf_cpts;
}

/**
* Post type registration arguments sanitizer.
*
Expand Down Expand Up @@ -561,34 +624,34 @@ private static function sanitize_args( $name, $args ) {
: 'post',
'map_meta_cap' => boolval( $args['map_meta_cap'] ?? true ),
'supports' => isset( $args['supports'] ) && is_array( $args['supports'] )
? array_map(
function ( $token ) {
return trim( $token );
},
array_filter(
$args['supports'],
static function (
$token
) {
$token = trim( $token );
$tokens = self::schema()['properties']['supports']['enum'];

return in_array( $token, $tokens, true );
}
)
? array_map(
function ( $token ) {
return trim( $token );
},
array_filter(
$args['supports'],
static function (
$token
) {
$token = trim( $token );
$tokens = self::schema()['properties']['supports']['enum'];

return in_array( $token, $tokens, true );
}
)
: self::schema()['properties']['supports']['default'],
)
: self::schema()['properties']['supports']['default'],
'taxonomies' => isset( $args['taxonomies'] ) && is_string( $args['taxonomies'] )
? implode(
',',
array_map(
function ( $tax ) {
return sanitize_text_field( trim( $tax ) );
},
explode( ',', $args['taxonomies'] )
)
? implode(
',',
array_map(
function ( $tax ) {
return sanitize_text_field( trim( $tax ) );
},
explode( ',', $args['taxonomies'] )
)
: array(),
)
: array(),
'has_archive' => boolval( $args['has_archive'] ?? false ),
'rewrite' => ! empty( $args['rewrite'] ) && is_string( $args['rewrite'] )
? sanitize_title( $args['rest_base'] )
Expand Down
33 changes: 31 additions & 2 deletions posts-bridge/includes/class-posts-bridge.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,18 @@ class Posts_Bridge extends Base_Plugin {
/**
* Handle post types REST Controller instances
*
* @var array<REST_Controller> $rest_controllers Array of active REST_Controller instances
* @var array<\WP_REST_Controller>
*/
private static $rest_controllers = array();

/**
* Handle a boolean value indicating if Posts Bridge should support ACF custom fields and
* post types.
*
* @var bool
*/
private static $acf_support = false;

/**
* Initialized addons, sets up plugin hooks and run db migrations if db version mismatches
* the plugin version.
Expand Down Expand Up @@ -152,6 +160,27 @@ function ( $plugin_data, $response ) {
10,
2
);

add_action(
'init',
static function () {
if ( defined( 'ACF_VERSION' ) ) {
self::$acf_support = function_exists( 'acf_get_field_groups' )
&& function_exists( 'acf_get_fields' );
}
},
9,
0,
);
}

/**
* Public proxy to the static acf_support attribute.
*
* @return bool
*/
public static function acf_support() {
return self::$acf_support;
}

/**
Expand Down Expand Up @@ -201,7 +230,7 @@ protected static function init() {
}

/**
* Enqueue admin client scripts
* Enqueue admin client scripts.
*/
private static function admin_enqueue_scripts() {
$version = self::version();
Expand Down
18 changes: 18 additions & 0 deletions posts-bridge/includes/class-posts-synchronizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -624,6 +624,8 @@ private static function sync_bridge( $bridge, $mode, $queue ) {
return false;
}

$custom_fields = Remote_CPT::custom_fields( $post_type );

$remote_pairs = array();
foreach ( $foreign_ids as $id ) {
$remote_pairs[ $id ] = 0;
Expand Down Expand Up @@ -731,13 +733,29 @@ private static function sync_bridge( $bridge, $mode, $queue ) {
Logger::log( "Remote CPT({$post_type}) #{$foreign_id} remote data after mappers" );
Logger::log( $post_data );

$meta_input = &$post_data['meta_input'];
$acf_fields = array();

foreach ( $custom_fields as $cf ) {
if ( ! $cf['_acf'] || ! isset( $meta_input[ $cf['name'] ] ) ) {
continue;
}

$acf_fields[ $cf['name'] ] = $meta_input[ $cf['name'] ];
unset( $meta_input[ $cf['name'] ] );
}

$post_id = wp_insert_post( $post_data );

if ( is_wp_error( $post_id ) || ! $post_id ) {
Logger::log( 'Post creation error on synchronization', Logger::ERROR );
throw new Exception( 'insert_error', 500 );
}

foreach ( $acf_fields as $field_name => $field_value ) {
update_field( $field_name, $field_value, $post_id );
}

update_post_meta( $post_id, Remote_CPT::FOREIGN_KEY_HANDLE, $foreign_id );

if ( isset( $post_data['featured_media'] ) ) {
Expand Down
Loading
Loading