By Christopher Ambler on Monday, 12 August 2019
Posted in Technical Issues
Likes 0
Views 521
Votes 0
I just need a pointer to documentation - I've developed a component that creates a blog entry once per day. I'd like to have it post that entry - what's the proper way to do this?

It's a component, so I can call into EB code directly, or use an API if that's the right way - but it looks like the XMLRPC method is long gone.

So... given my PHP code and a nice, rich string of text, how do I create and post an entry in code?

Thanks!
Hey there,

I am really sorry for the delay of this reply as it is a public holiday for us here.

If you would like store a post under legacy doc type (e.g. create a post using Tiny-MCE editor), then you can try this following code :

$post = EB::post();

$data = new stdClass();
$data->title = JText::_('You have successfully installed EasyBlog');
$data->permalink = 'easyblog-installed-successfully';
$data->content = 'your content';
$data->intro = 'your intro content';
$data->created_by = 892;
$data->created = EB::date()->toSql();
$data->ip = '127.0.0.1';
$data->frontpage = true;
$data->published = EASYBLOG_POST_PUBLISHED;
$data->hits = 1;
$data->access = 0;
$data->doctype = 'legacy';
$data->source_type = 'easyblog.sitewide';
$post->create();

$post->bind($data);
$options = array('validateData' => false,
'logUserIpAddress' => false,
'applyDateOffset' => false);

$post->save($options);
exit;


If you would like to store a post under ebd type (e.g. build-in composer editor), then you have take a look this file how we create a sample post for this. JoomlaFolder/administrator/components/com_easyblog/setup/controllers/install.post.php (look at this function createSamplePost ).
·
Monday, 12 August 2019 09:52
·
0 Likes
·
0 Votes
·
0 Comments
·
Perfect! Thank you!
·
Monday, 12 August 2019 10:00
·
0 Likes
·
0 Votes
·
0 Comments
·
You're welcome.

Hm, I noticed above code as what I shared, it truncated it from $post = EB: post(); to $post = EB:ost(); , remember follow this code from the php file https://take.ms/YwukL .
·
Monday, 12 August 2019 10:03
·
0 Likes
·
0 Votes
·
0 Comments
·
Unfortunately, this does not work.

The reason is that there are ACL checks on the create() and save() methods.

I can override this on create() with the code, below, but save() calls for ACL no matter what. Since this code is executed in a cron job, there is no logged-in user, thus your code fails the ACL check.



// Adding the checkAcl option allows create() to work, otherwise it would fail the ACL test
$post->create(array('checkAcl' => false));

$post->bind($data);

$options = array('validateData' => false,
'logUserIpAddress' => false,
'applyDateOffset' => false);

// This call, however, fails! It throws an exception saying that the user cannot save posts.
$post->save($options);
·
Monday, 12 August 2019 10:38
·
0 Likes
·
0 Votes
·
0 Comments
·
Confirmed. Adding this code makes it work (username and password redacted):


$app = JFactory::getApplication();
$app->login(Array('username' => "xxxxxx", 'password' => 'xxxxxxx'));


But, of course, I have very little desire to hard-code my admin user's name and password into my code!
·
Monday, 12 August 2019 10:58
·
0 Likes
·
0 Votes
·
0 Comments
·
You can try download my attached file how to skip those checking.
·
Monday, 12 August 2019 11:20
·
0 Likes
·
0 Votes
·
0 Comments
·
This seems to work!

Now I have only one other problem

When I autogenerate the post as a draft ( $data['published'] = EASYBLOG_POST_DRAFT; ), I can't delete it. My drafts are starting to fill up. Clicking delete (red X) does nothing, and going into the draft and using the delete draft button also does nothing.

Yikes!
·
Monday, 12 August 2019 11:54
·
0 Likes
·
0 Votes
·
0 Comments
·
Is this the button you click and it does nothing? https://take.ms/aTPsA

If yes, it seems like that is bug, from what I know is, when you click "Move to Trash" button from composer, it will move this draft to trash but it will still showing on the draft unless you click this delete button from the draft listing page https://take.ms/viIMj , then it will remove this draft on the site directly.

I will log this issue into our issue tracker.
·
Tuesday, 13 August 2019 11:52
·
0 Likes
·
0 Votes
·
0 Comments
·
Correct.

Since you're logging the issue, I'll call this resolved. Thanks!
·
Tuesday, 13 August 2019 14:10
·
0 Likes
·
0 Votes
·
0 Comments
·
You're most welcome, we will include the fix in next release version.

Just for your information, I have locked and marked this thread as resolved to avoid confusions in the future. Please start a new thread if you have any other issue in the future so it will be easier for us to manage your inquiries.

Thanks for understanding.
·
Tuesday, 13 August 2019 14:15
·
0 Likes
·
0 Votes
·
0 Comments
·
View Full Post