Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixes for issue #1001 #1019

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) ) {
Expand Down Expand Up @@ -654,6 +655,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,
Expand Down Expand Up @@ -977,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'] ) );
Expand Down
26 changes: 26 additions & 0 deletions includes/rest-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'] ) );
Expand Down Expand Up @@ -330,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,
Expand Down
32 changes: 32 additions & 0 deletions includes/template-tags.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down