From 7cae0350c9661e853e93e01e402f6ad08b30c667 Mon Sep 17 00:00:00 2001 From: muhammadtvk Date: Tue, 21 Feb 2023 12:13:12 +0400 Subject: [PATCH 1/2] fixes for issue #1001 fixes for issue #1001 --- .../WordPressExternalConnection.php | 1 + includes/rest-api.php | 4 +++ includes/template-tags.php | 32 +++++++++++++++++++ 3 files changed, 37 insertions(+) diff --git a/includes/classes/ExternalConnections/WordPressExternalConnection.php b/includes/classes/ExternalConnections/WordPressExternalConnection.php index 8560f12bc..528d2149a 100644 --- a/includes/classes/ExternalConnections/WordPressExternalConnection.php +++ b/includes/classes/ExternalConnections/WordPressExternalConnection.php @@ -654,6 +654,7 @@ public function push( $post_id, $args = array() ) { 'excerpt' => $post->post_excerpt, 'distributor_original_source_id' => $this->id, 'distributor_original_site_name' => get_bloginfo( 'name' ), + 'distributor_original_site_lang' => get_bloginfo( 'language' ), 'distributor_original_site_url' => home_url(), 'distributor_original_post_url' => get_permalink( $post_id ), 'distributor_remote_post_id' => $post_id, diff --git a/includes/rest-api.php b/includes/rest-api.php index 3d05ee2ea..1022dc593 100644 --- a/includes/rest-api.php +++ b/includes/rest-api.php @@ -86,6 +86,10 @@ function process_distributor_attributes( $post, $request, $update ) { if ( ! empty( $request['distributor_original_site_name'] ) ) { update_post_meta( $post->ID, 'dt_original_site_name', sanitize_text_field( $request['distributor_original_site_name'] ) ); } + + if ( ! empty( $request['distributor_original_site_lang'] ) ) { + update_post_meta( $post->ID, 'dt_original_site_lang', sanitize_text_field( $request['distributor_original_site_lang'] ) ); + } if ( ! empty( $request['distributor_original_site_url'] ) ) { update_post_meta( $post->ID, 'dt_original_site_url', sanitize_text_field( $request['distributor_original_site_url'] ) ); diff --git a/includes/template-tags.php b/includes/template-tags.php index 0c1e2e3ea..5323bccfa 100644 --- a/includes/template-tags.php +++ b/includes/template-tags.php @@ -100,6 +100,38 @@ function distributor_get_original_site_name( $post_id = null ) { } } +/** + * Get original site language + * + * @param int $post_id Leave null to use current post + * @since 1.0 + * @return string|bool + */ +function distributor_get_original_site_lang( $post_id = null ) { + if ( null === $post_id ) { + global $post; + + $post_id = $post->ID; + } + + $original_blog_id = get_post_meta( $post_id, 'dt_original_blog_id', true ); + $original_site_lang = get_post_meta( $post_id, 'dt_original_site_lang', true ); + + if ( ! empty( $original_blog_id ) && is_multisite() ) { + switch_to_blog( $original_blog_id ); + + $lang_code = get_bloginfo( 'language' ); + + restore_current_blog(); + + return $lang_code; + } elseif ( ! empty( $original_site_lang ) ) { + return $original_site_lang; + } else { + return false; + } +} + /** * See docblock for distributor_get_original_site_name * From 96140240f1e2cfa77e047d9510f568930c34871a Mon Sep 17 00:00:00 2001 From: muhammadtvk Date: Wed, 22 Feb 2023 13:27:27 +0400 Subject: [PATCH 2/2] fixes for #1019 for pull content fixes for #1019 for pull content --- .../WordPressExternalConnection.php | 2 ++ includes/rest-api.php | 22 +++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/includes/classes/ExternalConnections/WordPressExternalConnection.php b/includes/classes/ExternalConnections/WordPressExternalConnection.php index 528d2149a..ae82b5500 100644 --- a/includes/classes/ExternalConnections/WordPressExternalConnection.php +++ b/includes/classes/ExternalConnections/WordPressExternalConnection.php @@ -549,6 +549,7 @@ public function pull( $items ) { update_post_meta( $new_post, 'dt_syndicate_time', time() ); update_post_meta( $new_post, 'dt_original_post_url', esc_url_raw( $post_array['link'] ) ); update_post_meta( $new_post, 'dt_original_site_name', sanitize_text_field( $post_array['original_site_name'] ) ); + update_post_meta( $new_post, 'dt_original_site_lang', sanitize_text_field( $post_array['original_site_lang'] ) ); update_post_meta( $new_post, 'dt_original_site_url', sanitize_text_field( $post_array['original_site_url'] ) ); if ( ! empty( $post->post_parent ) ) { @@ -978,6 +979,7 @@ private function to_wp_post( $post ) { $obj->terms = ( ! empty( $post['distributor_terms'] ) ) ? $post['distributor_terms'] : []; $obj->media = ( ! empty( $post['distributor_media'] ) ) ? $post['distributor_media'] : []; $obj->original_site_name = ( ! empty( $post['distributor_original_site_name'] ) ) ? $post['distributor_original_site_name'] : null; + $obj->original_site_lang = ( ! empty( $post['distributor_original_site_lang'] ) ) ? $post['distributor_original_site_lang'] : null; $obj->original_site_url = ( ! empty( $post['distributor_original_site_url'] ) ) ? $post['distributor_original_site_url'] : null; $obj->full_connection = ( ! empty( $post['full_connection'] ) ); diff --git a/includes/rest-api.php b/includes/rest-api.php index 1022dc593..18b541511 100644 --- a/includes/rest-api.php +++ b/includes/rest-api.php @@ -334,6 +334,28 @@ function register_endpoints() { ), ) ); + + register_rest_field( + $post_types, + 'distributor_original_site_lang', + array( + 'get_callback' => function( $post_array ) { + $site_lang = get_post_meta( $post_array['id'], 'dt_original_site_lang', true ); + + if ( ! $site_lang ) { + $site_lang = get_bloginfo( 'language' ); + } + + return esc_html( $site_lang ); + }, + 'update_callback' => function( $value, $post ) { }, + 'schema' => array( + 'description' => esc_html__( 'Original site name for Distributor.', 'distributor' ), + 'type' => 'string', + ), + ) + ); + register_rest_field( $post_types,