コード例 #1
0
        public string AddPost(string blogid, string username, string password, Post post, bool publish)
        {
            this.ValidateUser(username, password);

            string data = this.ProcessPostData(blogid, username, password, post, null);

            return data;
        }
コード例 #2
0
        private string ProcessPostData(string blogId, string username, string password, Post post, int? postId)
        {
            string title = HttpUtility.HtmlDecode(post.title);
            string body = post.description;

            PostDto newPost = !postId.HasValue || postId.Value < 1
                                  ? new PostDto()
                                  : this.postService.GetPostByKey(postId.Value);

            newPost.Title = title;
            newPost.Slug = post.wp_slug;
            newPost.Content = body;

            if (!string.IsNullOrEmpty(post.mt_excerpt))
            {
                newPost.Excerpt = post.mt_excerpt;
            }
            else
            {
                newPost.Excerpt = (!string.IsNullOrEmpty(post.description))
                                      ? post.description.CleanHtmlText().Trim().Replace("&nbsp;", string.Empty).Cut(250)
                                      : string.Empty;
            }
            if (newPost.IsTransient)
            {
                newPost.PublishAt = (post.dateCreated == DateTime.MinValue || post.dateCreated == DateTime.MaxValue)
                                        ? DateTimeOffset.Now
                                        : new DateTime(post.dateCreated.Ticks, DateTimeKind.Utc);
            }
            else
            {
                newPost.PublishAt = (post.dateCreated == DateTime.MinValue || post.dateCreated == DateTime.MaxValue)
                                        ? newPost.PublishAt
                                        : new DateTime(post.dateCreated.Ticks, DateTimeKind.Utc);
            }

            BlogConfigurationDto blogConfiguration = this.configurationService.GetConfiguration();

            newPost.Status = newPost.PublishAt.UtcDateTime <= TimeZoneInfo.ConvertTimeFromUtc(DateTime.Now.ToUniversalTime(), blogConfiguration.TimeZone)
                                 ? ItemStatus.Published
                                 : ItemStatus.Scheduled;

            if (post.categories != null)
            {
                IList<string> categories = post.categories.ToList();
                IList<string> categoriesToAdd = new List<string>();

                IList<CategoryDto> allCategories = this.categoryService.GetCategories();

                foreach (string c in categories)
                {
                    CategoryDto cat = allCategories.FirstOrDefault(x => x.Name == c);
                    if (cat != null)
                    {
                        categoriesToAdd.Add(cat.Name);
                    }
                }

                newPost.Categories = categoriesToAdd.ToArray();
            }

            IEnumerable<string> tags = this.ExtractTags(post);

            if (tags != null && tags.Any())
            {
                newPost.Tags = tags.ToArray();
            }

            this.ConvertAllowCommentsForItem(post.mt_allow_comments, newPost);

            // set the author if specified, otherwise use the user that is authenticated by wlw
            // once a post is done, only the 'poster' can modify it
            if (!string.IsNullOrEmpty(post.wp_author_id))
            {
                // get the list of members
                WpAuthor[] authors = this.WpGetAuthors(blogId, username, password);
                int authorId = Convert.ToInt32(post.wp_author_id);
                string author = authors.First(a => a.user_id == authorId).user_login;
                newPost.Author = author;
            }
            else if (!string.IsNullOrEmpty(post.userid))
            {
                // get the list of members
                WpAuthor[] authors = this.WpGetAuthors(blogId, username, password);
                int authorId = Convert.ToInt32(post.userid);
                string author = authors.First(a => a.user_id == authorId).user_login;
                newPost.Author = author;
            }
            else
            {
                newPost.Author = username;
            }

            this.postService.SaveOrUpdate(newPost);

            return newPost.Id.ToString(CultureInfo.InvariantCulture);
        }
コード例 #3
0
        private Post GetMetaweblogPost(PostDto item, IEnumerable<WpAuthor> authors)
        {
            string authorId = this.GetAuthorId(item, authors);

            // the fields reported here are all the ones you should use
            Post p = new Post
                         {
                             dateCreated = item.CreatedAt.DateTime.ToUniversalTime(),
                             userid = authorId,
                             postid = item.Id.ToString(),
                             description = item.Content,
                             title = item.Title,
                             link = this.urlBuilder.Post.Permalink(item),
                             permalink = this.urlBuilder.Post.Permalink(item),
                             categories = item.Categories.ToArray(),
                             mt_excerpt = item.Excerpt,
                             mt_text_more = string.Empty,
                             mt_allow_comments = item.AllowComments
                                                     ? 1
                                                     : 2, // open or close
                             mt_allow_pings = 1,
                             mt_keywords = string.Join(",", item.Tags),
                             wp_slug = item.Slug,
                             wp_password = string.Empty,
                             wp_author_id = authorId,
                             wp_author_display_name = item.Author,
                             date_created_gmt = item.CreatedAt.DateTime.ToUniversalTime(),
                             post_status = item.Status.ToString(),
                             custom_fields = null,
                             sticky = false
                         };
            return p;
        }
コード例 #4
0
 private string[] ExtractTags(Post post)
 {
     string[] tagsFromBody = TagHelper.RetrieveTagsFromBody(post.description);
     string[] tagsPosed = !string.IsNullOrEmpty(post.mt_keywords)
                              ? post.mt_keywords.Split(new[]
                                                           {
                                                               ';', ','
                                                           })
                              : new string[] { };
     string[] tags = new string[tagsFromBody.Length + tagsPosed.Length];
     tagsFromBody.CopyTo(tags, 0);
     tagsPosed.CopyTo(tags, tagsFromBody.Length);
     return tags;
 }
コード例 #5
0
        public bool UpdatePost(string postid, string username, string password, Post post, bool publish)
        {
            this.ValidateUser(username, password);

            this.ProcessPostData(string.Empty, username, password, post, postid.ToInt32(0));

            return true;
        }
コード例 #6
0
        public Post[] GetRecentPosts(string blogid, string username, string password, int numberOfPosts)
        {
            this.ValidateUser(username, password);

            IPagedResult<PostDto> posts = this.postService.GetPosts(1, numberOfPosts, new ItemQueryFilter
                                                                                          {
                                                                                              MaxPublishAt = DateTimeOffset.Now.AddYears(1),
                                                                                              MinPublishAt = DateTimeOffset.Now.AddYears(-10)
                                                                                          });

            Post[] items = new Post[posts.Result.Count()];

            WpAuthor[] authors = this.WpGetAuthors(string.Empty, username, password);

            int i = 0;
            foreach (PostDto post in posts.Result)
            {
                items[i] = this.GetMetaweblogPost(post, authors);
                i++;
            }

            return items;
        }