/** * 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->insert_relations($blog_id, $related); }
/** * Moves site relations from deprecated site options to the new custom network table. * * @return void */ private function import_site_relations() { // TODO: With WordPress 4.6 + 2, just use `get_sites()`, and remove `$is_pre_4_6`. $is_pre_4_6 = version_compare($GLOBALS['wp_version'], '4.6-RC1', '<'); $all_sites = $is_pre_4_6 ? wp_get_sites() : get_sites(); foreach ($all_sites as $site) { // TODO: With WordPress 4.6 + 2, just use `$site->id`. $site_id = $is_pre_4_6 ? $site['blog_id'] : $site->id; $linked = get_blog_option($site_id, 'inpsyde_multilingual_blog_relationship', []); if ($linked) { $this->site_relations->insert_relations($site_id, $linked); } delete_blog_option($site_id, 'inpsyde_multilingual_blog_relationship'); } }
/** * @param array $site_option * @return void */ private function show_blog_relationships($site_option) { if (!is_array($site_option)) { return; } unset($site_option[$this->blog_id]); if (empty($site_option)) { return; } ?> <tr class="form-field"> <th scope="row"><?php esc_html_e('Relationships', 'multilingual-press'); ?> </th> <td> <?php foreach ($site_option as $blog_id => $meta) { $blog_id = (int) $blog_id; // Get blog display name switch_to_blog($blog_id); $blog_name = get_bloginfo('Name'); restore_current_blog(); // Get current settings $related_blogs = $this->relations->get_related_site_ids($this->blog_id); $checked = checked(TRUE, in_array($blog_id, $related_blogs), FALSE); $id = 'related_blog_' . $blog_id; ?> <p> <label for="<?php echo esc_attr($id); ?> "> <input id="<?php echo esc_attr($id); ?> " <?php echo esc_attr($checked); ?> type="checkbox" name="related_blogs[]" value="<?php echo esc_attr($blog_id); ?> " /> <?php echo esc_html($blog_name); ?> - <?php echo esc_html(\Inpsyde\MultilingualPress\get_site_language($blog_id, false)); ?> </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.', 'multilingual-press'); ?> </p> </td> </tr> <?php }
/** * Save the post to the blogs * * @param int $post_id * @param WP_Post $post * * @return void */ public function save($post_id, WP_Post $post) { if (!$this->is_valid_save_request($post)) { return; } $available_blogs = get_site_option('inpsyde_multilingual'); if (empty($available_blogs)) { return; } // auto-drafts $post_id = $this->basic_data->get_real_post_id($post_id); $post_type = $this->basic_data->get_real_post_type($post); $source_blog_id = get_current_blog_id(); $featured_image_path = $this->get_featured_image_path($post_id); $related_blogs = $this->relations->get_related_site_ids($source_blog_id); if (empty($related_blogs)) { return; } // Check Post Type if (!in_array($post_type, $this->allowed_post_types, true)) { return; } $this->save_context = ['source_blog' => get_current_blog_id(), 'source_post' => $post, 'real_post_type' => $post_type, 'real_post_id' => $post_id]; $this->basic_data->set_save_context($this->save_context); $post_meta = $this->basic_data->get_post_meta_to_transfer(); $this->basic_data->find_post_parents($post_type, $post->post_parent); /** * Runs before the first save_post action is called for the remote blogs. * * @param array $save_context Context of the to-be-saved post. */ do_action('mlp_before_post_synchronization', $this->save_context); foreach ($this->post_request_data[$this->name_base] as $remote_blog_id => $post_data) { if (!in_array($remote_blog_id, $related_blogs) || !\Inpsyde\MultilingualPress\site_exists($remote_blog_id)) { continue; } $nonce = $this->nonce_factory->create(["save_translation_of_post_{$post_id}_for_site_{$remote_blog_id}"]); $request_validator = Mlp_Save_Post_Request_Validator_Factory::create($nonce); if (!$request_validator->is_valid($post)) { continue; } switch_to_blog($remote_blog_id); $this->save_context['target_blog_id'] = $remote_blog_id; $new_post = $this->create_post_to_send($post_data, $post_type, $remote_blog_id); if ([] !== $new_post) { $sync_thumb = !empty($post_data['thumbnail']); $update = !empty($post_data['remote_post_id']) && 0 < $post_data['remote_post_id']; $new_id = $this->sync_post($new_post, $post_id, $remote_blog_id, $update); $this->basic_data->set_save_context($this->save_context); $this->basic_data->update_remote_post_meta($new_id, $post_meta); if ($sync_thumb && $featured_image_path) { $this->copy_thumb($new_id, $featured_image_path); } $tax_data = empty($post_data['tax']) ? [] : (array) $post_data['tax']; $this->set_remote_tax_terms($new_id, $tax_data); } restore_current_blog(); } /** * Runs after all save_post actions have been called for the remote blogs. * * @param array $save_context */ do_action('mlp_after_post_synchronization', $this->save_context); }
/** * Ask for specific translations with arguments. * * * @see prepare_translation_arguments() * * @param array $args { * * Optional. If left out, some magic happens. * * @type int $site_id Base site * @type int $content_id post or term_taxonomy ID, *not* term ID * @type string $type @see Mlp_Language_Api::get_request_type() * @type bool $strict When TRUE (default) only matching exact * translations will be included * @type string $search_term If you want to translate a search * @type string $post_type For post type archives * @type bool $include_base Include the base site in returned list * * } * @return Translation[] Array of Mlp_Translation instances, site IDs are the keys */ public function get_translations(array $args = []) { $arguments = $this->prepare_translation_arguments($args); $key = md5(serialize($arguments)); $cached = wp_cache_get($key, 'mlp'); if (is_array($cached)) { return $cached; } $sites = $this->site_relations->get_related_site_ids($arguments['site_id'], $arguments['include_base']); if (empty($sites)) { return []; } $content_relations = []; if (!empty($arguments['content_id'])) { // array with site_ids as keys, content_ids as values $content_relations = $this->content_relations->get_relations($arguments['site_id'], $arguments['content_id'], $arguments['type']); if (empty($content_relations) && $arguments['strict']) { return []; } } $translations = []; $languages = $this->get_all_language_data(); foreach ($sites as $site_id) { if (!isset($languages[$site_id])) { continue; } $translations[$site_id] = ['remote_title' => '', 'source_site_id' => $arguments['site_id'], 'target_site_id' => $site_id, 'target_content_id' => 0, 'type' => $arguments['type']]; } reset($translations); /** @type WP_Rewrite $wp_rewrite */ global $wp_rewrite; foreach ($translations as $site_id => &$arr) { $valid = TRUE; if (!empty($content_relations[$site_id])) { $content_id = $content_relations[$site_id]; $arr['target_content_id'] = $content_id; if ('term' === $arguments['type']) { $term_translation = new Mlp_Term_Translation($this->wpdb, $wp_rewrite, $this->type_factory); $translation = $term_translation->get_translation($content_id, $site_id); if (!$translation) { $valid = FALSE; } else { $arr = array_merge($arr, $translation); } } elseif ('post' === $arguments['type']) { switch_to_blog($site_id); $translation = $this->get_post_translation($content_relations[$site_id], $arguments['strict']); if (!$translation) { $valid = FALSE; } else { $arr = array_merge($arr, $translation); } restore_current_blog(); } } else { switch_to_blog($site_id); if ('search' === $arguments['type']) { $arr['remote_url'] = $this->type_factory->create_url([get_search_link($arguments['search_term'])]); } elseif ('post_type_archive' === $arguments['type'] && !empty($arguments['post_type'])) { $translation = $this->get_post_type_archive_translation($arguments['post_type']); $arr = array_merge($arr, $translation); } // Nothing found, use fallback if allowed if (empty($arr['remote_url']) && !$arguments['strict'] || 'front_page' === $arguments['type']) { $arr['remote_url'] = $this->type_factory->create_url([get_site_url($site_id, '/')]); } if (empty($arr['remote_url'])) { $valid = FALSE; } restore_current_blog(); } if (!$valid) { unset($translations[$site_id]); continue; } $data = $languages[$site_id]; if (!isset($data['http_name'])) { if (isset($data['lang'])) { $data['http_name'] = $data['lang']; } else { $data['http_name'] = ''; } } if ('' !== $data['http_name']) { $arr['icon_url'] = \Inpsyde\MultilingualPress\get_flag_url_for_site($site_id); } else { $arr['icon_url'] = $this->type_factory->create_url(['']); } $arr['suppress_filters'] = $arguments['suppress_filters']; $arr = $this->type_factory->create_translation([$arr, $this->type_factory->create_language([$data])]); } /** * Filter the translations before they are used. * * @param Translation[] $translations Translations. * @param array $arguments Translation arguments. */ $translations = apply_filters('mlp_translations', $translations, $arguments); wp_cache_set($key, $translations, 'mlp'); // TODO: In deprecated class, add "target_*" aliases for elements in $translations with "remote_*" keys. return $translations; }
/** * @param $blog_id * @param $old_related * @param $new_related * @param $relations * @param $changed * @return int */ private function delete_unset_relations($blog_id, $old_related, $new_related, SiteRelations $relations, $changed) { // Delete removed relations. foreach ($old_related as $old_blog_id) { if (!in_array($old_blog_id, $new_related)) { $relations->delete_relation($blog_id, $old_blog_id); $changed += 1; } } return $changed; }
/** * Renders the widget's view. * * @since 3.0.0 * * @param mixed $object Queried object, or other stuff. * @param array $instance Widget settings. * * @return void */ public function render($object, array $instance) { $related_site_ids = $this->site_relations->get_related_site_ids(); if (!$related_site_ids) { return; } $have_untranslated_posts = false; ?> <table class="widefat"> <?php foreach ($related_site_ids as $related_site_id) { ?> <?php switch_to_blog($related_site_id); ?> <?php $untranslated_posts = $this->post_repository->get_untranslated_posts(); ?> <?php if ($untranslated_posts) { ?> <?php $have_untranslated_posts = true; ?> <tr> <th colspan="3"> <strong> <?php /* translators: %s: site name */ $message = __('Pending Translations for %s', 'multilingual-press'); printf($message, get_bloginfo('name')); ?> </strong> </th> </tr> <?php foreach ($untranslated_posts as $post) { ?> <tr> <td style="width: 20%;"> <?php edit_post_link(__('Translate', 'multilingual-press'), '', '', $post->ID); ?> </td> <td style="width: 55%;"> <?php echo esc_html(get_the_title($post->ID)); ?> </td> <td style="width: 25%;"> <?php echo esc_html(get_the_time(get_option('date_format'), $post->ID)); ?> </td> </tr> <?php } ?> <?php } ?> <?php restore_current_blog(); ?> <?php } ?> <?php if (!$have_untranslated_posts) { ?> <tr> <td colspan="3"> <?php esc_html_e('No untranslated posts found.', 'multilingual-press'); ?> </td> </tr> <?php } ?> </table> <?php }