Inpsyde\MultilingualPress\Relations\Post\RelationshipContext::source_post PHP Method

source_post() public method

Returns the source post object.
Since: 3.0.0
public source_post ( ) : WP_Pos\WP_Post | null
return WP_Pos\WP_Post | null Source post object.
    public function source_post()
    {
        static $source_post = false;
        if (false === $source_post) {
            $source_site_id = $this->source_site_id();
            if (!$source_site_id) {
                $source_post = null;
                return null;
            }
            $source_post_id = $this->source_post_id();
            if (!$source_post_id) {
                $source_post = null;
                return null;
            }
            switch_to_blog($source_site_id);
            $source_post = get_post($source_post_id);
            restore_current_blog();
        }
        return $source_post;
    }

Usage Example

 /**
  * Connects the current post with a new remote one.
  *
  * @return bool|WP_Error Whether or not the relationship was updated successfully, or an error object.
  */
 private function connect_new_post()
 {
     $source_post = $this->context->source_post();
     if (!$source_post) {
         return false;
     }
     $remote_site_id = $this->context->remote_site_id();
     $save_context = ['source_blog' => $this->context->source_site_id(), 'source_post' => $source_post, 'real_post_type' => $this->get_real_post_type($source_post), 'real_post_id' => empty($_POST['post_ID']) ? $this->context->source_post_id() : (int) $_POST['post_ID']];
     /** This action is documented in inc/advanced-translator/Mlp_Advanced_Translator_Data.php */
     do_action('mlp_before_post_synchronization', $save_context);
     switch_to_blog($remote_site_id);
     $new_post_id = wp_insert_post(['post_type' => $source_post->post_type, 'post_status' => 'draft', 'post_title' => $this->context->new_post_title()], true);
     restore_current_blog();
     $save_context['target_blog_id'] = $remote_site_id;
     /** This action is documented in inc/advanced-translator/Mlp_Advanced_Translator_Data.php */
     do_action('mlp_after_post_synchronization', $save_context);
     if (is_wp_error($new_post_id)) {
         $this->last_error = $new_post_id;
         return false;
     }
     $this->context = RelationshipContext::from_existing($this->context, [RelationshipContext::KEY_NEW_POST_ID => $new_post_id]);
     return $this->connect_existing_post();
 }
All Usage Examples Of Inpsyde\MultilingualPress\Relations\Post\RelationshipContext::source_post