/**
  * Initializes the AJAX-based live search.
  *
  * @since 3.0.0
  *
  * @return void
  */
 public function initialize()
 {
     add_action('wp_ajax_' . self::ACTION, function () {
         $context = RelationshipContext::from_request();
         ob_start();
         $this->search_results->render($context);
         wp_send_json_success(['html' => ob_get_clean(), 'remoteSiteId' => $context->remote_site_id()]);
     });
 }
 /**
  * Disconnects the current post with the one given in the request.
  *
  * @return void
  */
 private function disconnect_post()
 {
     $remote_site_id = $this->context->remote_site_id();
     $remote_post_id = $this->context->remote_post_id();
     $source_site_id = $this->context->source_site_id();
     $translation_ids = $this->content_relations->get_translation_ids($this->context->source_site_id(), $remote_site_id, $this->context->source_post_id(), $remote_post_id, 'post');
     if ($translation_ids['ml_source_blogid'] !== $source_site_id) {
         $remote_site_id = $source_site_id;
         if (0 !== $remote_post_id) {
             $remote_post_id = $this->context->source_post_id();
         }
     }
     $this->content_relations->delete_relation($translation_ids['ml_source_blogid'], $remote_site_id, $translation_ids['ml_source_elementid'], $remote_post_id, 'post');
 }
 /**
  * Returns the latest/best-matching posts with respect to the given context.
  *
  * @since 3.0.0
  *
  * @param RelationshipContext $context Relationship context data object.
  *
  * @return WP_Post[] The latest/best-matching posts.
  */
 public function get_posts(RelationshipContext $context)
 {
     $remote_site_id = $context->remote_site_id();
     if (!$remote_site_id) {
         return [];
     }
     $source_post = $context->source_post();
     if (!$source_post) {
         return [];
     }
     $args = ['numberposts' => 10, 'post_type' => $source_post->post_type, 'post_status' => ['draft', 'future', 'private', 'publish']];
     $remote_post_id = $context->remote_post_id();
     if ($remote_post_id) {
         $args['exclude'] = $remote_post_id;
     }
     $search_query = $this->get_search_query();
     if ($search_query) {
         $args = array_merge($args, ['s' => $search_query, 'orderby' => 'relevance']);
     }
     switch_to_blog($remote_site_id);
     $posts = get_posts($args);
     restore_current_blog();
     return $posts;
 }
    /**
     * Renders the markup.
     *
     * @since   3.0.0
     * @wp-hook TODO: Reference (to-be-defined) class constant of Translation meta box.
     *
     * @param RelationshipContext $context Relationship context data object.
     *
     * @return void
     */
    public function render(RelationshipContext $context)
    {
        $remote_post_id = $context->remote_post_id();
        $remote_site_id = $context->remote_site_id();
        $action_container_id = "mlp-rc-action-container-{$remote_site_id}";
        $actions = [$this->default_action => __('Leave as is', 'multilingual-press'), RelationshipController::ACTION_CONNECT_NEW => __('Create new post', 'multilingual-press')];
        if ($remote_post_id) {
            $actions[RelationshipController::ACTION_DISCONNECT] = __('Remove relationship', 'multilingual-press');
        }
        $action_search_id = "mlp-rc-action-{$remote_site_id}-search";
        $search_container_id = "mlp-rc-search-container-{$remote_site_id}";
        $search_input_id = "mlp-rc-search-{$remote_site_id}";
        ?>
		<div class="mlp-relationship-control" data-remote-post-id="<?php 
        echo esc_attr($remote_post_id);
        ?>
"
			data-remote-site-id="<?php 
        echo esc_attr($remote_site_id);
        ?>
"
			data-results-selector="#mlp-rc-search-results-<?php 
        echo esc_attr($remote_site_id);
        ?>
"
			data-source-post-id="<?php 
        echo esc_attr($context->source_post_id());
        ?>
"
			data-source-site-id="<?php 
        echo esc_attr($context->source_site_id());
        ?>
">
			<button type="button" name="mlp_rc_<?php 
        echo esc_attr($remote_site_id);
        ?>
"
				class="button secondary mlp-rc-button mlp-click-toggler"
				data-toggle-target="#<?php 
        echo esc_attr($action_container_id);
        ?>
">
				<?php 
        esc_html_e('Change Relationship', 'multilingual-press');
        ?>
			</button>
			<div id="<?php 
        echo esc_attr($action_container_id);
        ?>
" class='hidden'>
				<div class="mlp-rc-settings">
					<div class="mlp-rc-actions">
						<?php 
        array_walk($actions, [$this, 'render_radio_input'], $remote_site_id);
        ?>
						<p>
							<label for="<?php 
        echo esc_attr($action_search_id);
        ?>
">
								<input
									type="radio"
									name="mlp_rc_action[<?php 
        echo esc_attr($remote_site_id);
        ?>
]"
									value="<?php 
        echo esc_attr(RelationshipController::ACTION_CONNECT_EXISTING);
        ?>
"
									id="<?php 
        echo esc_attr($action_search_id);
        ?>
"
									class="mlp-state-toggler"
									data-toggle-target="#<?php 
        echo esc_attr($search_container_id);
        ?>
">
								<?php 
        esc_html_e('Select existing post &hellip;', 'multilingual-press');
        ?>
							</label>
						</p>
					</div>
					<div id="<?php 
        echo esc_attr($search_container_id);
        ?>
" class="mlp-rc-search-container">
						<label for="<?php 
        echo esc_attr($search_input_id);
        ?>
">
							<?php 
        esc_html_e('Live search', 'multilingual-press');
        ?>
						</label>
						<input type="search" id="<?php 
        echo esc_attr($search_input_id);
        ?>
" class="mlp-rc-search">
						<ul id="mlp-rc-search-results-<?php 
        echo esc_attr($remote_site_id);
        ?>
"
							class="mlp-rc-search-results">
							<?php 
        $this->search_results->render($context);
        ?>
						</ul>
					</div>
				</div>
				<p>
					<button type="button" class="button button-primary mlp-save-relationship-button">
						<?php 
        esc_attr_e('Save and reload this page', 'multilingual-press');
        ?>
					</button>
					<span class="description">
						<?php 
        esc_html_e('Please save other changes first separately.', 'multilingual-press');
        ?>
					</span>
				</p>
			</div>
		</div>
		<?php 
        $this->localize_script();
    }