/**
  * Filters the nav menu items.
  *
  * @wp-hook wp_nav_menu_objects
  *
  * @param WP_Post[] $items Nav menu items.
  *
  * @return WP_Post[]
  */
 public function filter_items(array $items)
 {
     $translations = $this->language_api->get_translations(['strict' => false, 'include_base' => true]);
     foreach ($items as $key => $item) {
         if ($this->maybe_delete_obsolete_item($item)) {
             unset($items[$key]);
             continue;
         }
         if ($translations) {
             $this->prepare_item($item, $translations);
         }
     }
     return $items;
 }
 /**
  * Query the language API for translations and cache the result.
  *
  * @return array
  */
 private function get_translations()
 {
     if (array('failed') === $this->translations) {
         return array();
     }
     /** @var Mlp_Translation_Interface[] $translations */
     $translations = $this->language_api->get_translations(array('include_base' => TRUE));
     if (empty($translations)) {
         $this->translations = array('failed');
         return array();
     }
     $prepared = array();
     foreach ($translations as $translation) {
         $language = $translation->get_language();
         $language_name = $language->get_name('http');
         $url = $translation->get_remote_url();
         if ($url) {
             $prepared[$language_name] = $url;
         }
     }
     if (empty($prepared)) {
         $this->translations = array('failed');
         return array();
     }
     $this->translations = $prepared;
     return $this->translations;
 }
 /**
  * Updating Site Relations for new sites.
  *
  * @param  int $blog_id
  * @return int Number of affected rows.
  */
 private function update_relation($blog_id)
 {
     if (empty($_POST['related_blogs'])) {
         return 0;
     }
     $new_related = (array) $_POST['related_blogs'];
     $related = array_map('intval', $new_related);
     return $this->site_relation->set_relation($blog_id, $related);
 }
    /**
     * Print tab content and provide two hooks.
     *
     * @return void
     */
    public function render_content()
    {
        ?>
		<form action="<?php 
        print $this->page_data->get_form_action();
        ?>
" method="post">
			<input type="hidden" name="action" value="<?php 
        echo $this->page_data->get_action_name();
        ?>
" />
			<input type="hidden" name="id" value="<?php 
        echo $this->blog_id;
        ?>
" />
			<?php 
        wp_nonce_field($this->page_data->get_nonce_action(), $this->page_data->get_nonce_name());
        $siteoption = get_site_option('inpsyde_multilingual', array());
        $languages = $this->language_api->get_db()->get_items(array('page' => -1));
        print '<table class="form-table mlp-admin-settings-table">';
        $this->show_language_options($siteoption, $languages);
        $this->show_blog_relationships($siteoption, $languages);
        /**
         * Runs at the end of but still inside the site settings table.
         *
         * @param int $blog_id Blog ID.
         */
        do_action('mlp_blogs_add_fields', $this->blog_id);
        if (has_action('mlp_blogs_add_fields_secondary')) {
            _doing_it_wrong('mlp_blogs_add_fields_secondary', 'mlp_blogs_add_fields_secondary is deprecated, use mlp_blogs_add_fields instead.', '2.1');
        }
        /**
         * @see mlp_blogs_add_fields
         * @deprecated
         */
        do_action('mlp_blogs_add_fields_secondary', $this->blog_id);
        print '</table>';
        submit_button();
        ?>
		</form>
		<?php 
    }
 /**
  * Returns the translations.
  *
  * @return Translation[]
  */
 private function get_translations()
 {
     if (!is_singular()) {
         return [];
     }
     if ($this->translations) {
         return $this->translations;
     }
     $this->translations = $this->language_api->get_translations(['type' => 'post']);
     return $this->translations;
 }
    /**
     * Print tab content and provide two hooks.
     *
     * @return void
     */
    public function render_content()
    {
        $languages = $this->language_api->get_db()->get_items(array('page' => -1));
        ob_start();
        ?>
		<h3>MultilingualPress</h3>
		<table class="form-table">
			<tr class="form-field">
				<td>
					<label for="inpsyde_multilingual_lang">
						<?php 
        esc_html_e('Language', 'multilingualpress');
        ?>
					</label>
				</td>
				<td>
					<select name="inpsyde_multilingual_lang" id="inpsyde_multilingual_lang">
						<option value="-1"><?php 
        esc_html_e('Choose language', 'multilingualpress');
        ?>
</option>
						<?php 
        foreach ($languages as $language) {
            // missing HTTP code
            if (empty($language->http_name)) {
                continue;
            }
            echo '<option value="' . esc_attr($language->http_name) . '">' . $language->english_name . '/' . $language->native_name . '</option>';
        }
        ?>
					</select>
				</td>
			</tr>
			<tr class="form-field">
				<td>
					<label for="inpsyde_multilingual_text">
						<?php 
        esc_html_e('Alternative language title', 'multilingualpress');
        ?>
					</label>
				</td>
				<td>
					<input class="regular-text" type="text" id="inpsyde_multilingual_text" name="inpsyde_multilingual_text" />
					<p class="description"><?php 
        esc_html_e('Enter a title here that you want to be displayed in the frontend instead of the default one (i.e. "My English Site")', 'multilingualpress');
        ?>
</p>
				</td>
			</tr>
			<tr class="form-field">
				<td>
					<label for="inpsyde_multilingual_text">
						<?php 
        esc_html_e('Relationships', 'multilingualpress');
        ?>
					</label>
				</td>
				<td><?php 
        $site_option = get_site_option('inpsyde_multilingual', array());
        foreach ($site_option as $blog_id => $meta) {
            $blog_id = (int) $blog_id;
            // Get blog display name
            switch_to_blog($blog_id);
            $blog_name = esc_html(get_bloginfo('Name'));
            restore_current_blog();
            $id = "related_blog_{$blog_id}";
            ?>
						<p>
							<label for="<?php 
            echo $id;
            ?>
">
								<input style="width:auto;" id="<?php 
            echo $id;
            ?>
" type="checkbox" name="related_blogs[]" value="<?php 
            echo $blog_id;
            ?>
" />
								<?php 
            echo $blog_name;
            ?>
 - <?php 
            echo Mlp_Helpers::get_blog_language($blog_id);
            ?>
							</label>
						</p>
						<?php 
        }
        ?>
					<p class="description">
						<?php 
        esc_html_e('You can connect this site only to sites with an assigned language. Other sites will not show up here.', 'multilingualpress');
        ?>
					</p>
				</td>
			</tr>

			<?php 
        do_action('mlp_after_new_blog_fields');
        ?>
		</table>
		<?php 
        $template = ob_get_contents();
        ob_end_clean();
        ?>
		<script>
			( function( $ ) {
				$(document).ready( function(){

					var submit      = $( 'form' ).find( '.submit' ),
						template    = '<?php 
        echo str_replace("\n", "", $template);
        ?>
'
					;

					submit.before( template );

				} );
			} )( jQuery );
		</script>

		<?php 
    }
    /**
     * Prints the template for the MultilingualPress table, and fires an action to inject markup.
     *
     * @wp-hook admin_footer
     *
     * @return void
     */
    public function print_template()
    {
        global $hook_suffix;
        if ('site-new.php' !== $hook_suffix) {
            return;
        }
        $db = $this->language_api->get_db();
        $languages = $db->get_items(array('page' => -1));
        ?>
		<script type="text/html" id="mlp-add-new-site-template">
			<h3>
				<?php 
        esc_html_e('MultilingualPress', 'multilingual-press');
        ?>
			</h3>
			<table class="form-table">
				<tr class="form-field">
					<td>
						<label for="mlp-site-language">
							<?php 
        esc_html_e('Language', 'multilingual-press');
        ?>
						</label>
					</td>
					<td>
						<select name="inpsyde_multilingual_lang" id="mlp-site-language" autocomplete="off">
							<option value="-1">
								<?php 
        esc_html_e('Choose language', 'multilingual-press');
        ?>
							</option>
							<?php 
        foreach ($languages as $language) {
            ?>
								<?php 
            $this->render_language_option($language);
            ?>
							<?php 
        }
        ?>
						</select>
					</td>
				</tr>
				<tr class="form-field">
					<td>
						<label for="inpsyde_multilingual_text">
							<?php 
        esc_html_e('Alternative language title', 'multilingual-press');
        ?>
						</label>
					</td>
					<td>
						<input type="text" name="inpsyde_multilingual_text" class="regular-text"
							id="inpsyde_multilingual_text">
						<p class="description">
							<?php 
        esc_html_e('Enter a title here that you want to be displayed in the frontend instead of the default one (i.e. "My English Site")', 'multilingual-press');
        ?>
						</p>
					</td>
				</tr>
				<tr class="form-field">
					<td>
						<label for="inpsyde_multilingual_text">
							<?php 
        esc_html_e('Relationships', 'multilingual-press');
        ?>
						</label>
					</td>
					<td>
						<?php 
        $this->render_relationships();
        ?>
						<p class="description">
							<?php 
        esc_html_e('You can connect this site only to sites with an assigned language. Other sites will not show up here.', 'multilingual-press');
        ?>
						</p>
					</td>
				</tr>
				<?php 
        /**
         * Runs at the end but still inside the new blog fields table.
         */
        do_action('mlp_after_new_blog_fields');
        ?>
			</table>
		</script>
		<?php 
    }