Ejemplo n.º 1
0
 function blog_shortcode($atts, $content)
 {
     extract(shortcode_atts(array('id' => ''), $atts));
     $out = $content;
     if (empty($id) || !blog_exists($id)) {
         return;
     }
     switch_to_blog($id);
     $out = do_ccs_shortcode($out);
     restore_current_blog();
     return $out;
 }
Ejemplo n.º 2
0
 /**
  * @param int     $post_id
  * @param WP_Post $post
  * @return void
  */
 public function save($post_id, WP_Post $post)
 {
     if (!$this->request_validator->is_valid($post)) {
         return;
     }
     $post_type = $this->get_real_post_type($post);
     $post_id = $this->get_real_post_id($post_id);
     if (!in_array($post_type, $this->allowed_post_types)) {
         return;
     }
     if (empty($this->post_request_data['mlp_to_translate'])) {
         return;
     }
     $to_translate = $this->post_request_data['mlp_to_translate'];
     $this->save_context = array('source_blog' => get_current_blog_id(), 'source_post' => $post, 'real_post_type' => $post_type, 'real_post_id' => $post_id);
     // Get the post
     $post_data = get_post($post_id, ARRAY_A);
     $post_meta = $this->get_post_meta_to_transfer($post_id);
     /**
      * Pre-Filter before Saving the Post
      * @param   Array $post_data
      * @param   Array $save_context
      */
     $post_data = apply_filters('mlp_pre_save_post', $post_data, $this->save_context);
     // When the filter returns FALSE, we'll stop here
     if (FALSE == $post_data || !is_array($post_data)) {
         return;
     }
     $file = $path = '';
     $fileinfo = array();
     // Check for thumbnail
     if (current_theme_supports('post-thumbnails')) {
         $thumb_id = get_post_thumbnail_id($post_id);
         if (0 < $thumb_id) {
             $path = wp_upload_dir();
             $file = get_post_meta($thumb_id, '_wp_attached_file', TRUE);
             $fileinfo = pathinfo($file);
             include_once ABSPATH . 'wp-admin/includes/image.php';
             //including the attachment function
         }
     }
     // Create the post array
     $new_post = array('post_title' => $post_data['post_title'], 'post_content' => $post_data['post_content'], 'post_status' => 'draft', 'post_author' => $post_data['post_author'], 'post_excerpt' => $post_data['post_excerpt'], 'post_date' => $post_data['post_date'], 'post_type' => $post_data['post_type']);
     $this->find_post_parents($post_data['post_type'], $post->post_parent);
     /**
      * Run before the first save_post action is called in other blogs.
      *
      * @param Array $save_context
      */
     do_action('mlp_before_post_synchronization', $this->save_context);
     // Create a copy of the item for every related blog
     foreach ($to_translate as $blog_id) {
         if ($blog_id == get_current_blog_id() or !blog_exists($blog_id)) {
             continue;
         }
         switch_to_blog($blog_id);
         // Set the linked parent post
         $new_post['post_parent'] = $this->get_post_parent($blog_id);
         $this->save_context['target_blog_id'] = $blog_id;
         /**
          * Filter post data before it is saved to the database.
          *
          * @param array $new_post
          * @param array $context
          */
         $new_post = apply_filters('mlp_pre_insert_post', $new_post, $this->save_context);
         // Insert remote blog post
         $remote_post_id = wp_insert_post($new_post);
         //echo $remote_post_id . '<br>';
         if (!empty($post_meta)) {
             $this->update_remote_post_meta($remote_post_id, $post_meta);
         }
         if ('' != $file) {
             // thumbfile exists
             if (0 < count($fileinfo)) {
                 $filedir = wp_upload_dir();
                 $filename = wp_unique_filename($filedir['path'], $fileinfo['basename']);
                 $copy = copy($path['basedir'] . '/' . $file, $filedir['path'] . '/' . $filename);
                 if ($copy) {
                     $wp_filetype = wp_check_filetype($filedir['url'] . '/' . $filename);
                     //get the file type
                     $attachment = array('post_mime_type' => $wp_filetype['type'], 'guid' => $filedir['url'] . '/' . $filename, 'post_parent' => $remote_post_id, 'post_title' => '', 'post_excerpt' => '', 'post_author' => $post_data['post_author'], 'post_content' => '');
                     //insert the image
                     $attach_id = wp_insert_attachment($attachment, $filedir['path'] . '/' . $filename);
                     if (!is_wp_error($attach_id)) {
                         wp_update_attachment_metadata($attach_id, wp_generate_attachment_metadata($attach_id, $filedir['path'] . '/' . $filename));
                         set_post_thumbnail($remote_post_id, $attach_id);
                     }
                     // update the image data
                 }
             }
         }
         $this->set_linked_element($post_id, $blog_id, $remote_post_id);
         restore_current_blog();
     }
     /**
      * Run after all save_post actions are called in other blogs.
      *
      * @param Array $save_context
      */
     do_action('mlp_after_post_synchronization', $this->save_context);
 }
 /**
  * Check if a blog id is for a linked, existing blog.
  *
  * @param array $titles
  * @param int   $blog_id
  * @return bool
  */
 private function is_valid_blog_id(array $titles, $blog_id)
 {
     return isset($titles[$blog_id]) && blog_exists($blog_id);
 }
 /**
  * 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();
     $thumb_data = $this->get_source_thumb_data($post_id);
     $related_blogs = $this->relations->get_related_sites($source_blog_id);
     if (empty($related_blogs)) {
         return;
     }
     // Check Post Type
     if (!in_array($post_type, $this->allowed_post_types)) {
         return;
     }
     $this->save_context = array('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 (!blog_exists($remote_blog_id) || !in_array($remote_blog_id, $related_blogs)) {
             continue;
         }
         $nonce_validator = Mlp_Nonce_Validator_Factory::create("save_translation_of_post_{$post->ID}_for_site_{$remote_blog_id}", $source_blog_id);
         $request_validator = Mlp_Save_Post_Request_Validator_Factory::create($nonce_validator);
         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 (array() !== $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 && $thumb_data->has_thumb) {
                 $this->copy_thumb($new_id, $thumb_data);
             }
             $tax_data = empty($post_data['tax']) ? array() : (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);
 }
 /**
  * Save the post to the blogs
  *
  * @global         $wpdb
  * @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();
     $thumb_data = $this->get_source_thumb_data($post_id);
     $related_blogs = $this->relations->get_related_sites($source_blog_id, FALSE);
     if (empty($related_blogs)) {
         return;
     }
     // Check Post Type
     if (!in_array($post_type, $this->allowed_post_types)) {
         return;
     }
     $this->basic_data->save_context = array('source_blog' => get_current_blog_id(), 'source_post' => $post, 'real_post_type' => $post_type, 'real_post_id' => $post_id);
     $post_meta = $this->basic_data->get_post_meta_to_transfer();
     $this->basic_data->find_post_parents($post_type, $post->post_parent);
     /**
      * Run before the first save_post action is called in other blogs.
      *
      * @param Array $save_context
      */
     do_action('mlp_before_post_synchronization', $this->save_context);
     foreach ($this->post_request_data[$this->name_base] as $remote_blog_id => $post_data) {
         if (!blog_exists($remote_blog_id) or !in_array($remote_blog_id, $related_blogs)) {
             continue;
         }
         switch_to_blog($remote_blog_id);
         $this->basic_data->save_context['target_blog_id'] = $remote_blog_id;
         $new_post = $this->create_post_to_send($post_data, $post_type, $remote_blog_id);
         if (array() !== $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->update_remote_post_meta($new_id, $post_meta);
             if ($sync_thumb && $thumb_data->has_thumb) {
                 $this->copy_thumb($new_id, $thumb_data);
             }
             if (!empty($post_data['tax'])) {
                 $this->set_remote_tax_terms($new_id, $post_data['tax']);
             }
         }
         restore_current_blog();
     }
     /**
      * Run after all save_post actions are called in other blogs.
      *
      * @param Array $save_context
      */
     do_action('mlp_after_post_synchronization', $this->save_context);
 }