I'm trying to insert data from a contact form (CF7) into two different table
I'm pretty sure my code is correct, but still nothing
Here's the code
remove_all_filters ('wpcf7_before_send_mail');
add_action( 'wpcf7_before_send_mail', 'my_conversion' );
function my_conversion( $cf7 )
{
$email = $cf7->posted_data["email"];
$name = $cf7->posted_data["lastname"];
$tel = $cf7->posted_data["cf_3"];
$fonction = $cf7->posted_data["cf_1"];
$entreprise = $cf7->posted_data["cf_2"];
$newsletter = $cf7->posted_data["newsletter"];
insert($email, $name, $tel, $fonction,$entreprise,$newsletter);
}
function insert($email, $name, $tel, $fonction,$entreprise,$newsletter)
{
global $wpdb;
$wpdb->insert("wp_wysija_user", array(
"user_id" => NULL,
"wpuser_id" =>"0",
"email" => $email,
"firstname" => "",
"lastname" => $name,
"ffonc" => $fonction,
"fent" => $entreprise,
"ftel" => $tel,
"ip" => "0",
"confirmed_ip" => NULL,
"confirmed_at" => NULL,
"last_opened" => NULL,
"last_clicked" => NULL,
"keyuser" => NULL,
"created_at" => "",
"status" => "0",
"domain" => ""
));
if($newsletter == "oui")
{
$wpdb->insert("wp_wysija_user_list", array(
"list" => "3",
"user_id" => NULL,
"sub_date" => "1430666348",
"unsub_date" => "0"
));
}
}
I know there's another plugin that does the job, but I'd rather do it my way
Thanks,
Jeremie.
Steps to perform:
Turn on WP Debug and see if you get some obvious error or warning
Check if the function my_conversion is called e.g. write
echo "I am called"; die;
Most probably the wpdb->insert is not working
Try the following:
var_dump( $wpdb->last_query );
or
$wpdb->print_error()
As you are most probably doin all this within an ajax request make sure to write the debugging results into a logfile or make sure you can see the results on your screen or within your console.
Related
I am using the Codeigniter4 framework and I have a controller function that displays a form with name, email, and message fields and then on submission validates the input. I am trying to set up 2 test cases using the built-in PHPUnit test suite for this controller: one with all errors and one with no errors. The issue is that after I run the first test with the errors, the second test always fails with the same errors being saved. I have tried destroying the session, but because the test suite uses MockSession, the destroy() doesn't seem to work the same.
Here is the function that displays the form:
public function question()
{
$items = array(
"name" => array("display" => "Your Name", "type" => "input"),
"email" => array("display" => "Your Email", "type" => "input"),
"message" => array("display" => "Message", "type" => "textarea", "rows" => 5),
);
$data = $this->getForm($items, array(), "How Can We Help?");
$this->displayView("template/form", $data);
}
Here is the function that validates input after form submission, displays message to user, and redirect back to form:
public function attemptQuestion()
{
$rules = array(
"name" => array(
"rules" => "required|string|max_length[30]",
"errors" => array(
"required" => "Your name is required",
"max_length" => "Please limit to the first 30 characters in your name"
)
),
"email" => array(
"rules" => "required|valid_email|max_length[30]",
"errors" => array(
"required" => "Your email is required",
"valid_email" => "Your email is invalid",
"max_length" => "Email address is limited to 30 characters"
)
),
"message" => array(
"rules" => "required",
"errors" => array(
"required" => "Message is required"
)
),
);
if (! $this->validate($rules))
{
return redirect()->to(site_url("contact/question"))->withInput()->with("errors", $this->validator->getErrors());
}
return redirect()->to(site_url("contact/question"))->with("message", lang("Base.messageSent"));
}
And here is the test class:
namespace CodeIgniter;
use CodeIgniter\Test\CIUnitTestCase;
use CodeIgniter\Test\DatabaseTestTrait;
use CodeIgniter\Test\FilterTestTrait;
use CodeIgniter\Test\FeatureTestTrait;
class QuestionTest extends CIUnitTestCase
{
use FilterTestTrait;
public function testQuestionError()
{
$params = array(
"name" => "01234567890123456789012345678901234567", // >30 characters
"email" => "01234567890123456789012345678901234567", // >30 characters, invalid email
"message" => NULL, // empty
csrf_token() => csrf_hash()
);
$errors = array(
'name' => 'Please limit to the first 100 characters in your name',
'email' => 'Your email is invalid',
'message' => 'Message is required'
);
$this->checkIndex($params, $errors);
}
public function testQuestionSuccess()
{
$params = array(
"name" => "Joe Tester",
"email" => "joe#example.com",
"message" => "this is a test",
csrf_token() => csrf_hash()
);
$this->checkIndex($params, FALSE, lang("Base.messageSent"));
}
private function checkQuestion($parameters, $errors = FALSE, $message = FALSE)
{
// clear session
$_SESSION = [];
$result = $this->post("contact/index", $parameters);
$result->assertOK();
$result->assertRedirect();
$result->assertRedirectTo(site_url("contact/question"));
if ($errors)
{
$result->assertSessionHas('errors', $errors);
}
if ($message)
{
$result->assertSessionHas('message', $message);
}
}
}
For reference, here are a couple links that are relevant, but still don't seem to fully answer the question:
https://github.com/codeigniter4/CodeIgniter4/issues/3578
https://forum.codeigniter.com/thread-74701.html
UPDATE
I've upgraded to CI4.2.0 which enables "resetServices" by default, which should fix the issue, but unfortunately, I get the same error. For reference, I've tried wrapping the $this->post call in print statements, like this:
file_put_contents('php://stderr', print_r($parameters, true));
$result = $this->post("contact/index", $parameters);
print_r($_SESSION);
And that produces the following results:
evana#LAPTOP-ICCPFR27 MINGW64 /c/sites/www (master)
$ ./vendor/bin/phpunit --filter 'ContactPostTest'
PHPUnit 9.5.20 #StandWithUkraine
Warning: No code coverage driver available
Array
(
[name] => 01234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789
[email] => 01234567890123456789012345678901234567890123456789012345678901234567890123456789089#example.com
[subject] =>
[csrfPEtoken] => 928d217bb8d905caa5783275a72347db
)
.Array
(
[_ci_old_input] => Array
(
[get] => Array
(
)
[post] => Array
(
)
)
[__ci_vars] => Array
(
[_ci_old_input] => new
[_ci_validation_errors] => new
[errors] => new
)
[_ci_validation_errors] => a:6:{s:4:"name";s:53:"Please limit to the first 100 characters in your name";s:5:"email";s:21:"Your email is invalid";s:7:"message";s:19:"Message is required";}
[errors] => Array
(
[name] => Please limit to the first 100 characters in your name
[email] => Your email is invalid
[message] => Message is required
)
)
Array
(
[name] => Evan
[email] => evan#example.com
[message] => this is a test
[csrfPEtoken] => a55091ab0a922566c44b3ede325b59cd
)
F 2 / 2 (100%)Array
(
[_ci_old_input] => Array
(
[get] => Array
(
)
[post] => Array
(
)
)
[__ci_vars] => Array
(
[_ci_old_input] => new
[_ci_validation_errors] => new
[errors] => new
)
[_ci_validation_errors] => a:6:{s:4:"name";s:53:"Please limit to the first 100 characters in your name";s:5:"email";s:21:"Your email is invalid";s:7:"message";s:19:"Message is required";}
[errors] => Array
(
[name] => Please limit to the first 100 characters in your name
[email] => Your email is invalid
[message] => Message is required
)
)
Time: 00:00.320, Memory: 16.00 MB
There was 1 failure:
1) CodeIgniter\ContactPostTest::testQuestionSuccess
'message' is not in the current $_SESSION
Failed asserting that an array has the key 'message'.
C:\sites\www\vendor\codeigniter4\framework\system\Test\TestResponse.php:255
C:\sites\www\tests\app\Controllers\ContactPostTest.php:101
C:\sites\www\tests\app\Controllers\ContactPostTest.php:67
FAILURES!
Tests: 2, Assertions: 7, Failures: 1.
To clear out the session, you need to use MockSession and enable resetServices. Also, in case it is helpful to anyone else, you need have only one HTTP request in each function (when I expanded my tests, I put all the error calls in one function, which caused issues with the session as well).
Here is the corrected code:
<?php
namespace CodeIgniter;
use CodeIgniter\Test\CIUnitTestCase;
use CodeIgniter\Test\FilterTestTrait;
use CodeIgniter\Test\FeatureTestTrait;
use CodeIgniter\Session\Handlers\ArrayHandler;
use CodeIgniter\Test\Mock\MockSession;
use Config\Services;
class ContactPostTest extends CIUnitTestCase
{
use FilterTestTrait, FeatureTestTrait;
protected $setUpMethods = [
'resetServices',
'mockSession',
];
public function testQuestionError()
{
$params = array(
"name" => "01234567890123456789012345678901234567", // >30 characters
"email" => "01234567890123456789012345678901234567", // >30 characters, invalid email
"message" => NULL, // empty
csrf_token() => csrf_hash()
);
$errors = array(
'name' => 'Please limit to the first 100 characters in your name',
'email' => 'Your email is invalid',
'message' => 'Message is required'
);
$this->checkQuestion($params, $errors);
}
public function testQuestionSuccess()
{
$params = array(
"name" => "Joe Tester",
"email" => "joe#example.com",
"message" => "this is a test",
csrf_token() => csrf_hash()
);
$this->checkQuestion($params, FALSE, lang("Base.messageSent"));
}
private function checkQuestion($parameters, $errors = FALSE, $message = FALSE)
{
$result = $this->post("contact", $parameters);
$result->assertOK();
$result->assertRedirect();
$result->assertRedirectTo(site_url("contact"));
if ($errors)
{
$result->assertSessionHas('errors', $errors);
}
if ($message)
{
$result->assertSessionHas('message', $message);
}
}
}
To be very specific - there is CRM system written in Codeigniter called Rise. I would like to make (automatically) an expense entry ( call save() method of an Expenses class ) each time someone logs in ( inside save_timelog() method of a Projects class ) time manually.
Expense Controller:
function save() {
validate_submitted_data(array(
"id" => "numeric",
"expense_date" => "required",
"category_id" => "required",
"amount" => "required"
));
$id = $this->input->post('id');
$target_path = get_setting("timeline_file_path");
$files_data = move_files_from_temp_dir_to_permanent_dir($target_path, "expense");
$has_new_files = count(unserialize($files_data));
$data = array(
"expense_date" => $this->input->post('expense_date'),
"title" => $this->input->post('title'),
"description" => $this->input->post('description'),
"category_id" => $this->input->post('category_id'),
"amount" => unformat_currency($this->input->post('amount')),
"project_id" => $this->input->post('expense_project_id'),
"user_id" => $this->input->post('expense_user_id'),
"files" => $files_data
);
<.. ETC. CHECKING FILES ..>
$save_id = $this->Expenses_model->save($data, $id);
if ($save_id) {
echo json_encode(array("success" => true, "data" => $this->_row_data($save_id), 'id' => $save_id, 'message' => lang('record_saved')));
} else {
echo json_encode(array("success" => false, 'message' => lang('error_occurred')));
}
}
Projects Controller:
function save_timelog() {
$this->access_only_team_members();
$id = $this->input->post('id');
$start_time = $this->input->post('start_time');
$end_time = $this->input->post('end_time');
$note = $this->input->post("note");
$task_id = $this->input->post("task_id");
if (get_setting("time_format") != "24_hours") {
$start_time = convert_time_to_24hours_format($start_time);
$end_time = convert_time_to_24hours_format($end_time);
}
$start_date_time = $this->input->post('start_date') . " " . $start_time;
$end_date_time = $this->input->post('end_date') . " " . $end_time;
$start_date_time = convert_date_local_to_utc($start_date_time);
$end_date_time = convert_date_local_to_utc($end_date_time);
$data = array(
"project_id" => $this->input->post('project_id'),
"start_time" => $start_date_time,
"end_time" => $end_date_time,
"note" => $note ? $note : "",
"task_id" => $task_id ? $task_id : 0,
);
if (!$id) {
//insert mode
$data["user_id"] = $this->input->post('user_id') ? $this->input->post('user_id') : $this->login_user->id;
} else {
//edit mode
//check edit permission
$this->check_timelog_updte_permission($id);
}
$save_id = $this->Timesheets_model->save($data, $id);
if ($save_id) {
echo json_encode(array("success" => true, "data" => $this->_timesheet_row_data($save_id), 'id' => $save_id, 'message' => lang('record_saved')));
} else {
echo json_encode(array("success" => false, 'message' => lang('error_occurred')));
}
}
So now what I'm trying to do is inside Projects controller, save_timelog() method just below these lines:
<...>
if (!$id) {
//insert mode
$data["user_id"] = $this->input->post('user_id') ? $this->input->post('user_id') : $this->login_user->id;
} else {
//edit mode
//check edit permission
$this->check_timelog_updte_permission($id);
}
/* CREATING A SAMPLE ARRAY WITH STATIC DATA FOR AN EXAMPLE EXPENSE ENTRY */
$a = array(
"expense_date" => '2018-03-13',
"title" => 'Cat Food',
"description" => 'Sheba, Felix, KiteKat',
"category_id" => '85',
"amount" => '500',
"project_id" => '84',
"user_id" => '10',
"files" => $files_data
);
/* TRYING TO SAVE/SEND EXAMPLE ARRAY TO Expenses Class save() method (?) */
$b = $this->Expenses_model->save($a);
/* RESULT (?) */
$save_id = $this->Timesheets_model->save($data, $id);
if ($save_id) {
echo json_encode(
array(
array(
"success" => true,
"data" => $this->_timesheet_row_data($save_id),
'id' => $save_id,
'message' => lang('record_saved')
),
array(
"success" => true,
"data" => _row_data($b),
'id' => $save_id,
'message' => lang('record_saved')
)
)
);
} else {
echo json_encode(array("success" => false, 'message' => lang('error_occurred')));
}
<.. Closing save_timelog() method ..>
However it surely doesn't work and all I get is "POST http://rise.test/index.php/projects/save_timelog 500 (Internal Server Error)".
I also load Expenses model and Expenses categories model in Projects _construct():
Projects Controller:
public function __construct() {
parent::__construct();
$this->load->model("Project_settings_model");
$this->load->model("Expense_categories_model");
$this->load->model("Expenses_model");
}
I also contacted developers of Rise with following question/answer:
Me:
In Projects controller save_timelog() method I just want to call
Expenses controller save() method, and if save_timelog() is successful
I would like to save an Expense ( $this->Expenses_model->save($data,
$id); ) with appropriate data. Could be static values for now for
$data array in save() method - just to find out it's working.
Rise Devs:
Hi, You are doing almost right. Just remove the 2nd parameter $id. It
should be used only for update. $this->Expenses_model->save($data)
Would really appreciate any help and directions! Thanks.
i am trying to create an inline bot for telegram with php. I have followed the steps with the BotFather. I have created the bot, taken the token, setinline and set the placeholder message. I have set the webhook and it's working. But when i type the bot in the message i do get nothing and if I send the message, just nothing happen. The webhook is working, I have tried it with normal messages.
This is my code, after a while I just give up and get it from a blog, edited it a bit.
$content = file_get_contents("php://input");
$update = json_decode($content, true);
$chatID = $update["message"]["chat"]["id"];
//sendMessage(print_r($update,true), $chatID);
if (isset($update["inline_query"])) {
$inlineQuery = $update["inline_query"];
$queryId = $inlineQuery["id"];
$queryText = $inlineQuery["query"];
if (isset($queryText) && $queryText !== "") {
apiRequestJson("answerInlineQuery", [
"inline_query_id" => $queryId,
"results" => ($queryText),
"cache_time" => 86400,
]);
}
else {
apiRequestJson("answerInlineQuery", [
"inline_query_id" => $queryId,
"results" => [
[
"type" => "article",
"id" => "0",
"title" => "TEST",
"message_text" => "TEST",
],
]
]);
}
}
The bot still show me nothing.
I think i just skipped a step.
The results need to have the key message_text inside the input_message_content.Therefore a result could look like this:
$results = array(
array(
"type" => "article",
"id" => "1",
"title" => "Title",
"description" => "Description",
"input_message_content" => array(
"message_text" => "<code>Message 1</code>",
"parse_mode" => "HTML"
)
)
);
$postData = array(
"inline_query_id" => $inlineQuery["id"],
"results" => json_encode($results),
"cache_time" => 0
);
I have integrated the Wepay payment gateway. But I have facing a problem to pass json object to wepay. It always shows a incorrect json format. Please look at the below code.
$forca_a = array(
'debit_opt_in'=>true
);
$forca = json_encode($forca_a,JSON_FORCE_OBJECT);
$wepay_create_array = array(
'name' =>"xxxx",
'description' => "xxxxxxxxx xxxx",
'callback_uri' => "xxxxxxx",
'country' => "CA",
'currencies' => array('CAD'),
'country_options' => $forca,
'rbits'=> array(
array(
'receive_time'=>strtotime("now"),
'type' =>'website_uri',
'source' => 'partner_database',
'properties'=> array('uri'=>xxxxx)
)
)
);
If I won't pass the country_options, its seems to working but If I pass this parameter, it always give me an error says "Incorrect JSON format".
I sent an email to wepay help center. They told me that, you are passing the string "country_options":"{"debit_opt_in":true}" <--- this is a string Instead of "country_options":{"debit_opt_in":true} <--- this is a JSON object. So I'm confused. I have no idea how do I pass the JSON object. There is only way and is json_encode($object).
hey use below code to get proper json
<?php
$forca_a = array(
'debit_opt_in'=>true
);
// $forca = json_encode($forca_a);
$wepay_create_array = array(
'name' =>"xxxx",
'description' => "xxxxxxxxx xxxx",
'callback_uri' => "xxxxxxx",
'country' => "CA",
'currencies' => array('CAD'),
'country_options' => $forca_a,
'rbits'=> array(
array(
'receive_time'=>strtotime("now"),
'type' =>'website_uri',
'source' => 'partner_database',
'properties'=> array('uri'=>'xxxxx')
)
)
);
print_r(json_encode($wepay_create_array));
?>
this code will give following json output
{
"name": "xxxx",
"description": "xxxxxxxxx xxxx",
"callback_uri": "xxxxxxx",
"country": "CA",
"currencies": ["CAD"],
"country_options": {
"debit_opt_in": true
},
"rbits": [{
"receive_time": 1461561030,
"type": "website_uri",
"source": "partner_database",
"properties": {
"uri": "xxxxx"
}
}]
}
You have no need to make:
$forca = json_encode($forca_a,JSON_FORCE_OBJECT);
before you put it to $wepay_create_array.
Before sending request, i think, you make json_encode($wepay_create_array), and yes, after that you will have 'string' for country_options key.
I don't know much about NuSoap, but unfortunately I have to use it.
I'm trying to follow example code for the dotmailer web service api Here. I need to be able to add custom fields for subscribers.
In the example code custom/additional fields are defined like so;
new SoapVar($FirstName,XSD_STRING,"string","http://www.w3.org/2001/XMLSchema");
Heres the code I have right now;
<?php
function subscribeUserEmail($email)
{
$username = "********";
$password = "********";
$postURL = "http://apiconnector.com/api.asmx?WSDL";
$contact = array (
"Email" => $email,
"AudienceType" => "B2C",
"OptInType" => "Single",
"EmailType" => "Html",
"ID" => -1,
"DataFields" => array(
"Keys" => array("TEST"),
"Values" => array("Name")
)
);
$params = array(
"username" => $username,
"password" => $password,
"contact" => $contact,
"addressbookId" => "******"
);
$client = new soapclient($postURL, true);
$error = $client->getError();
$result = $client->call('AddContactToAddressBook', $params);
echo "<h2>Request</h2>";
print("<pre>".$client->request."</pre>");
echo "<h2>Response</h2>";
print("<pre>".$client->response."</pre>");
echo "<h2>Debug</h2>";
print("<pre>".$client->debug_str."</pre>");
}
?>
If the $contact array is changed to this;
$contact = array (
"Email" => $email,
"AudienceType" => "B2C",
"OptInType" => "Single",
"EmailType" => "Html",
"ID" => -1,
"DataFields" => array(
"Keys" => array("TEST"),
"Values" => array(new SoapVar("Name",XSD_STRING,"string","http://www.w3.org/2001/XMLSchema") )
)
);
And used with a regular soap client, the code works. So I'm pretty sure there aren't any other problems with my function.
I've tried using the following;
new soapval("string", XSD_STRING, "Name","http://www.w3.org/2001/XMLSchema");
As an alternative to the SoapVar() method, but I get the same errors as if I entered the value as plain text.
How can I replicate the functionality of SoapVar() in NuSoap? This appears to be the only issue.