Updating post_meta in WordPress through XML-RPC - ruby

I am currently working on a project that involves scraping data from over 300 static pages and transferring that data into a WordPress site. I have set up various custom fields with the Simple Fields (http://simple-fields.com/) plugin, and have a basic XML-RPC connection working. Below is what I am using to test injecting custom posts (written in Ruby):
connection = XMLRPC::Client.new_from_uri "http://localhost:8888/xmlrpc.php"
username = "admin"
password = "password"
test_post = {
:post_type => "custom_property",
:post_status => "draft",
:post_title => "test post!",
:post_meta => {
:_simple_fields_fieldGroupID_1_fieldID_3_numInSet_0 => "test"
}
}
puts connection.call("wp.newPost", 1, username, password, test_post)
I have tried :custom_fields in place of :post_meta but still to no avail. I can't seem to be able to update the custom fields that have been created through Simple Fields.
If anyone has any suggestions on what to do, it would be greatly appreciated.
EDIT: This problem was solved by extending the XML-RPC functions using this article: http://kovshenin.com/2010/custom-xml-rpc-methods-in-wordpress/

It is actually quite tricky to figure out the structure of the Wordpress post. If it is a custom field you want to post in, you should use the format below:
'custom_fields' => [{'key' => 'something', 'value' => 'something else'}]
If it is custom taxonomy, use this:
'terms' => {
'custom_type' => [value],
'custom_something' => ['array_item','array_item']
}
For terms you always need to put the value into an array even if it's just one item. Hope it helped.

Related

Add cursorPaginate to collection

So, I have built a very detailed collection in Laravel to give me very specific and clean data in a format that works within my frontend. Now, forgetting that this will be a LOT of data, I needed to add paginate. As this is just going to be infinite scroll, I wanted to use cursorPaginate
Unfortunately, the code below does not allow it. The standard error of Method Illuminate\Support\Collection::cursorPaginate does not exist
How would I be best to refactor this to give me the data required AND give me cursorPaginate? I am completely open to changing some of it, I just do not know where best to start.
Thanks in advance!
return Activity::get()->groupBy('batch_uuid')->map(function ($batch) {
return [
'description' => $batch->first()->description,
'uuid' => $batch->first()->batch_uuid,
'event' => $batch->first()->event,
'created_at' => $batch->first()->created_at,
'subject' => $batch->first()->subject_type::find($batch->first()->subject_id),
'activities' => $batch->map(function ($activity) {
return [
'item' => $activity->causer_type::find($activity->causer_id),
];
}),
];
})->sortByDesc('created_at')->values();
```
You should watch this video from Laravel Daily. First method mentioned in this video he uses LengthAwarePaginator but you can try CursorPaginator for your purpose.

How can I validate GET controller params in CakePHP 2?

Given this on the model:
public $validate = [
'amount' => array(
'rule' => array('comparison', '>=', 0),
'message' => 'You must buy over 0 of this item!'
)
];
How can I validate param #2 of the below?
public function buy(int $item, int $amount) {
Validation seems to be built only for POST, which I'd like to opt out of here.
First things first, modifying the database with GET requests is an anti-pattern for many different reasons. Even if you assume a friendly user agent (which you never should!), browsers can behave quirky and do unexpected stuff like for example sending GET request multiple times (that is perfectly valid as GET is not ment to modify data), which they usually won't for POST/PUT/DELETE.
I would strongly suggest to change your endpoint to handle POST requests instead.
That being said, you can generally validate whatever you want, the validation mechanisms first and foremost just validate data, they don't know or care where it stems from. You can hand over whatever data you want to your model, and let it validate it:
$data = array(
'item' => $item,
'amount' => $amount,
);
$this->ModelName->set($data);
if ($this->ModelName->validates()) {
// data is valid
} else {
// data is invalid
$errors = $this->ModelName->validationErrors;
}
Moreover you can use CakePHP's validation methods completely manually too:
App::uses('Utility', 'Validation');
$isValid = Validation::comparison($amount, '>' 0);
This example of course doesn't make too much sense, given that $isValid = $amount > 0 would do the same, however it should just show that you can validate anything everywhere without models being involved.
See also
Cookbook > Models > Data Validation > Validating Data from the Controller
Cookbook > Models > Data Validation > Core Validation Rules

Conditionally loaded data in API resource: How to "pass" a condition?

I got a little issue to solve. In my app I am handling with a lot of Models and each model does have something like:
ModelResource
ModelResourceCollection
ModelResourceOverview
ModelResourceOverviewCollection
The reason is: Sometimes I don't need all the information that are visible if I am using the ModelResource - in this case I am calling the ModelResourceOverview.
Example
PostResource
- title
- tags
- content
- author
Example
PostOverviewResource
- title
- author
As I have a lot of Models I have a huge number of ApiResource-Classes and I want to get rid of that.
I thought about using $this->when in the Resource and pass something like "full" or "overview" to the request in the Controller.
Example
$data
= new PostResource($this->post);
So my question is: Is it the best way to add this to the request or is there a handier/nicer way to handle this?
Laravel has a way to hide fields on the fly: Hiding attributes from json
e.g.
return $post->makeHidden('content')->toArray();
If your logic about "visible or not" is bound to the current request, then you should use when or mergeWhen as you mentioned (everything here https://laravel.com/docs/7.x/eloquent-resources#conditional-attributes ), therefore you'll only have 2 resources instead of 4
public function toArray($request)
{
return [
'title' => $this->title,
'author' => $this->author,
$this->mergeWhen($this->needsFullData($request), [
'tags' => $this->tags,
'content' => $this->content,
]),
];
}
protected function needsFullData($request)
{
//Your logic
}

getOfferById method provided by an API does not work (invalid Id or Error cannot find parameter) [Laravel]

I am a junior developer working on this project.
I'm using an API who provide offers and I'm trying to display them.
They provide several methods as "getOffers" "search" "getOfferById".
I managed to display all the offers with the getOffers, but I can't use the getOfferById. I might be using it the wrong way.
They also provide us with a sandbox to play with the methods so when I put the id on the sandbox it works fine. Here is an exemple from the doc:
$offer = $soapClient->getOfferById($offerId, array('key' => 'length.unit', 'value' => 'm'));
and here is what I've tried so far :
$offerId = 2020012100009;
$offer = $soapClient->getOfferById($offerId, array('key' => 'length.unit', 'value' => 'm'));
dd($offer->length);
I get "Error can not find parameter" for this.
$offer = $soapClient->getOfferById($offerId);
and I get "Invalid Id" for this.
There is no such id in the database, are you sure that the id you providing is valid?
I resolved my problem. I had to apply it like that.
$offer = $soapClient->getOfferById(array(
'id' => $offerId
));

Codeigniter Datamapper not running get rules on fields with NULL values

I'm using Datamapper v1.8.2 with Codeigniter v2.1.2 and have a "get" rule that doesn't seem to run on fields with NULL values. Here's the model:
class Page extends Datamapper {
public $validation = array(
'name' => array(
'rules' => array('required'),
'get_rules' => array('get_page_name')
)
);
function _get_page_name($field)
{
$this->$field = 'TESTING '.$this->id;
}
}
Example code:
$page = new Page();
foreach ($page->get() as $p) echo $p->name;
When the table field name has any non-null value including an empty string it works fine outputting something like TESTING 358, but when the value is NULL (which is the default value for this field), it outputs nothing. There is no difference using get_iterated().
I guess I could work around this by changing the default value, but I'm wondering if I'm doing something wrong or missed something in the documentation, or maybe it's a bug? Does anyone know what the issue is?
Also, if someone could point me to the proper thread in the CI forums for Datamapper 1.8.2 support that would be great, I'm trying to find it and getting lost in a maze of links to threads for old versions of DM.
You need to add the allow_null to the get_rules array to make this work. I'm not sure about the intent of the creator but this is how get_rules are implemented (however i don't see it mentioned in the docs).

Resources