public async Task UpdatePage(int pageId, string neuerHtmlCode, WordPressCredentials credentials)
        {
            _logger.LogInformation("Starte WordPress-API");
            var config = new WordPressSiteConfig
            {
                BaseUrl  = credentials.BaseUrl,
                Username = credentials.Username,
                Password = credentials.Password
            };

            using (var client = new WordPressClient(config))
            {
                var post = new Post
                {
                    Id      = Convert.ToString(pageId),
                    Content = neuerHtmlCode
                };

                var sucess = await client.EditPostAsync(post);

                if (!sucess)
                {
                    throw new Exception($"EditPostAsync gab false zurück :(");
                }
            }
        }
Beispiel #2
0
 public WordPressHelper(string baseUrl, string userName, string password)
 {
     SiteConfig = new WordPressSiteConfig()
     {
         BaseUrl  = baseUrl,
         Username = userName,
         Password = password
     };
 }
Beispiel #3
0
 public WordPress(string username, string password, string site, int blogid)
 {
     config = new WordPressSiteConfig
     {
         BaseUrl  = site,
         BlogId   = blogid,
         Username = username,
         Password = password
     };
 }
 public SimpleWordPressClient(string baseUrl, string username, string password, int blogId, string author, string parentId)
 {
     _author   = author;
     _parentId = parentId;
     _wpConfig = new WordPressSiteConfig()
     {
         BaseUrl  = baseUrl,
         Username = username,
         Password = password,
         BlogId   = blogId
     };
     _client = new WordPressSharp.WordPressClient(_wpConfig);
 }
        private void MakeItHappen(string type, string statuts, string title, string content)
        {
            try
            {
                if (string.IsNullOrEmpty(tbUser.Text) || string.IsNullOrEmpty(tbPass.Text) || string.IsNullOrEmpty(tbURL.Text) ||
                    string.IsNullOrEmpty(tbTitle.Text) || string.IsNullOrEmpty(tbContent.Text))
                {
                    MessageBox.Show("Check inputs!");
                    return;
                }

                var post = new Post
                {
                    PostType        = type, // "post" or "page"
                    Title           = title,
                    Content         = content,
                    PublishDateTime = DateTime.UtcNow,
                    Status          = statuts // "draft" or "publish"
                };

                var cfg = new WordPressSiteConfig
                {
                    BaseUrl  = tbURL.Text.Trim(),
                    BlogId   = 1,
                    Username = tbUser.Text,
                    Password = tbPass.Text
                };

                using (var client = new WordPressClient(cfg))
                {
                    string idstr = client.NewPost(post);
                    int    id    = 0;

                    if (int.TryParse(idstr, out id) && id > 0)
                    {
                        MessageBox.Show("Success!");
                    }
                    else
                    {
                        MessageBox.Show("Failed!");
                    }
                }
            }
            catch (Exception e)
            {
                MessageBox.Show("Error occured. Check log for details.");
                File.WriteAllText("log.log", e.ToString());
            }
        }
Beispiel #6
0
        public static WordPressClient WordpressCL(string url, string usr, string pwd)
        {
            WordPressSiteConfig config = new WordPressSiteConfig
            {
                Username = usr,
                Password = pwd,
                BaseUrl  = url
            };
            WordPressClient client;

            try
            {
                client = new WordPressClient(config);
            }
            catch { return(null); }
            return(client);
        }
Beispiel #7
0
        public static void CreateAsync(PostCreateModel _post)
        {
            var config = new WordPressSiteConfig
            {
                Username = "******",
                Password = "******",
                BaseUrl  = "https://www.saigontown.com/",
                BlogId   = 1
            };

            using (var client = new WordPressClient(config))
            {
                List <Term> terms = new List <Term>();
                for (int i = 0; i < _post.tags.Count; i++)
                {
                    var nOC = _post.tags[i].Split(' ').Length;
                    if (nOC > 7)
                    {
                        continue;
                    }
                    var tag   = _post.tags[i];
                    var exist = client.GetTerms("post_tag", new TermFilter
                    {
                        Search = tag
                    });
                    if (exist.Length > 0)
                    {
                        terms.Add(exist[0]);
                    }
                    else
                    {
                        var term = new Term
                        {
                            Name        = tag,
                            Description = "term description",
                            Slug        = String.Join('_', tag.ToLower().Split(" ")),
                            Taxonomy    = "post_tag"
                        };
                        var termId = client.NewTerm(term);
                        term.Id = termId;
                        terms.Add(term);
                    }
                }

                for (int i = 0; i < _post.categories.Count; i++)
                {
                    terms.Add(new Term
                    {
                        Id       = _post.categories[i],
                        Taxonomy = "category"
                    });
                }

                var post = new Post
                {
                    PostType        = "post",
                    Title           = _post.title,
                    Content         = _post.content,
                    PublishDateTime = DateTime.Now,
                    Terms           = terms.ToArray(),
                    Status          = "publish",
                };
                string realImg = _post.img;
                if (!_post.Sggp)
                {
                    realImg = _post.img.Split("_")[0];
                    if (!realImg.Contains(".jpg") && !realImg.Contains(".jpeg"))
                    {
                        realImg += ".jpg";
                    }
                }
                // if (_post.Bds)
                // {
                //     client.NewPost(post);
                //     return;
                // }
                var featureImage = Data.CreateFromUrl(realImg);
                var media        = client.UploadFile(featureImage);
                post.FeaturedImageId = media.Id;

                var id = Convert.ToInt32(client.NewPost(post));
            }
        }
Beispiel #8
0
        static void Main(string[] args)
        {
            WordPressSiteConfig wordpressSiteConfig = new WordPressSiteConfig();

            wordpressSiteConfig.BaseUrl  = "https://virtualtales.com/";
            wordpressSiteConfig.Username = "******";
            wordpressSiteConfig.Password = @"MM0m#q81cP&X2BQuYKm$e";
            int addedCounter  = 0;
            int missedCounter = 0;

            using (var client = new WordPressClient(wordpressSiteConfig))
            {
                PostFilter postFilter = new PostFilter()
                {
                    PostType = "post"
                };                                                                 //записи с типом post
                Post[] posts = client.GetPosts(postFilter);
                for (int i = 0; i < posts.Length; i++)
                {
                    /*     if (!posts[i].Title.Contains("test"))
                     *   {
                     *       //пропускам не тестовые  --- !!!!!!!!!!!!!!!       УДАЛИТЬ     !!!!!!!!!!!!!!!
                     *       continue;
                     *   }
                     */
                    if (posts[i].HasFeaturedImage())
                    {
                        //пропускаем посты с картинкой
                        continue;
                        // posts[i].FeaturedImage = null;
                    }

                    List <string> tags = new List <string>();
                    foreach (Term term in posts[i].Terms)
                    {
                        if (term.Taxonomy == "post_tag")
                        {
                            tags.Add(term.Name);//лучше Slug
                        }
                    }

                    string fileId = string.Empty;
                    if (tags.Any())
                    {
                        IEnumerable <WebImage> webImages = GetWebImages(tags);
                        foreach (WebImage webImage in webImages)
                        {
                            try
                            {
                                Data featureImage = Data.CreateFromUrl(webImage.Url);
                                featureImage.post_id = int.Parse(posts[i].Id);
                                fileId = client.UploadFile(featureImage).Id;
                                if (!string.IsNullOrEmpty(fileId))
                                {
                                    pixabayAccessor.AddUsedImage(webImage);
                                    break;
                                }
                            }
                            catch (Exception)
                            {
                            }
                        }

                        posts[i].FeaturedImageId = fileId;
                        client.EditPost(posts[i]);
                        addedCounter++;
                    }
                    else
                    {
                        // не нашли ярлыков в посте
                        Console.WriteLine($"Post \"{posts[i].Title}\" has not any post_tags");
                        missedCounter++;
                    }
                }
            }

            Console.WriteLine($"new feature images added: {addedCounter}{Environment.NewLine}posts without post_tags: {missedCounter}");
            Console.WriteLine($"{Environment.NewLine}press any key.");
            Console.ReadLine();
        }