Get updated data from wp_insert_post - wordpress

i have problem with managing advanced custom fields (i have 4 of them). I want to check if some of them are empty when i add new post. I tried something like empty($_POST['name_of_post_meta']) but it does not work.
How can i catch
function wpse120996_add_custom_field_automatically($post_id)
{
global $wpdb;
if (!wp_is_post_revision($post_id)) {
$category = get_the_category($post_id);
$category = $category[0]->name;
$link = get_permalink($post_id);
if (($_POST['post_status'] == 'publish') && ($_POST['original_post_status'] != 'publish')) { // new post
$lector = get_post_meta($post_id, 'lektor_pl', true);
$subs_pl = get_post_meta($post_id, 'napisy_pl', true);
$orginal = get_post_meta($post_id, 'wersja_eng', true);
$subs_eng = get_post_meta($post_id, 'subs_eng', true);
if (empty($_POST['lektor_pl'])) {
$lector = 0;
}
if (empty($_POST['napisy_pl'])) {
$subs_pl = 0;
}
if (empty($_POST['wersja_eng'])) {
$orginal = 0;
}
if (empty($_POST['subs_eng'])) {
$subs_eng = 0;
}
if (!empty($_POST['lektor_pl'])) {
$lector = 1;
}
if (!empty($_POST['napisy_pl'])) {
$subs_pl = 1;
}
if (!empty($_POST['wersja_eng'])) {
$orginal = 1;
}
if (!empty($_POST['subs_eng'])) {
$subs_eng = 1;
}
$sql = $wpdb->prepare("INSERT INTO `wp_cron_notification` (`id`, `post_id`, `subb_pl` , `lector`, `subb_eng`, `orginal`,`link`, `category`) values (NULL, %s, %s, %s, %s, %s, %s, %s)", $post_id, $subs_pl, $lector, $subs_eng, $orginal, $link, $category);
$wpdb->query($sql) or die("ERROR #3");
}
}
}
add_action('wp_insert_post', 'wpse120996_add_custom_field_automatically', 1);
Thanks in advance for any help.

You can make use here of the ACF save_post function (http://www.advancedcustomfields.com/resources/acfsave_post/) which fires just after a post is saved. See more here:
If you put the following in your functions.php file you should be able to access the newly saved post and carry out code above. For example:
add_action('acf/save_post', 'post_save_update_title', 20);
function post_save_update_title($post_id){
//Your code here
}

Related

Import taxanomy terms

Hi I am importing a csv file, where one of the columns is representing the taxonomy term names separated with ; delimiter.
The code I have is the following:
public function save_post($post,$meta,$terms,$thumbnail,$is_update) {
// Separate the post tags from $post array
if (isset($post['post_tags']) && !empty($post['post_tags'])) {
$post_tags = $post['post_tags'];
unset($post['post_tags']);
}
// Special handling of attachments
if (!empty($thumbnail) && $post['post_type'] == 'attachment') {
$post['media_file'] = $thumbnail;
$thumbnail = null;
}
// Add or update the post
if ($is_update) {
$h = RSCSV_Import_Post_Helper::getByID($post['ID']);
$h->update($post);
} else {
$h = RSCSV_Import_Post_Helper::add($post);
}
// Set post tags
if (isset($post_tags)) {
$h->setPostTags($post_tags);
}
// Set meta data
$h->setMeta($meta);
// Set terms
foreach ($terms as $key => $value) {
$h->setObjectTerms($key, $value);
}
// Add thumbnail
if ($thumbnail) {
$h->addThumbnail($thumbnail);
}
return $h;
}
public function setObjectTerms($taxonomy, $terms)
{
$post = $this->getPost();
if ($post instanceof WP_Post) {
wp_set_object_terms($post->ID, $terms, $taxonomy);
} else {
$this->addError('post_is_not_set', __('WP_Post object is not set.', 'really-simple-csv-importer'));
}
}
The $key is the taxanomy ex: Col and the value is the actual name ex: Fiction Books
Any idea why the taxanomy terms are not ticked when the importation is complete?

How to remove or decrease the custom field value by -1

I have the following function in my wordpress site functions file
function setVideoHalfViews() {
global $wpdb;
if (!isset($_POST['data'])) {
exit;
}
$data = json_decode(stripslashes_deep($_POST['data']), true);
$halfViewsCount = $data['halfViewsCount'];
$currentPostId = $data['currentPostId'];
$count_key = 'halfViewsCount';
$viewsMeta = get_post_meta($currentPostId, $count_key, true);
$count_keydeletepartial = 'partialViewsCount';
$viewsMetadeletepartial = get_post_meta($currentPostId, $count_keydeletepartial, true);
if ($viewsMeta == '') {
add_post_meta($currentPostId, $count_key, $halfViewsCount, true);
} else {
$viewsMeta++;
update_post_meta($currentPostId, $count_key, $viewsMeta);
--$viewsMetadeletepartial;
}
$count_key2 = 'wrdp_half_fs';
$viewsMeta2 = get_post_meta($currentPostId, $count_key2, true);
if ($viewsMeta2 == '') {
add_post_meta($currentPostId, $count_key2, $videoViewsCount, true);
} else {
$viewsMeta2++;
update_post_meta($currentPostId, $count_key2, $viewsMeta2);
}
}
I want to decrease the $viewsMetadeletepartial custom field value by -1 by this code --$viewsMetadeletepartial. But it seems not working. Any idea why? I tried with $viewsMetadeletepartial-- also but the same result.

Migrating PHP Login into Wordpress?

So i am using a custom Login form with php using Sessions i would like to know how i can implement this into wordpress.
I have changed the database object to wpdb method successfully however i am now wondering how to implement the session on wordpress?
Is there a specific way or can i just create a session variable and check for that variable isset on the redirect page?
This is my current php login code without wpdb keep in mind i wont be using the classes:
$database = new MySQL();
$session = new Session($database);
$msg = '';
$msgl = '';
$msgemail = '';
$message = '';
if (isset($_POST['login']) && $session->isLogged == false) {
$email = $database->escapeString($_POST['user_email']);
$pass = sha1($database->escapeString($_POST['user_pass']));
$result = $database->executeQuery("SELECT user_id, user_type from `user-tbl` WHERE `billing_email`='$email' AND `account_password`='$pass'");
if ($database->numRows($result) == 1) {
$row = $database->fetchResult($result);
$_SESSION['account_id'] = $row['user_id'];
$_SESSION['account_type'] = $row['user_type'];
$_SESSION['security'] = hash('md5', $_SERVER['HTTP_USER_AGENT']);
if (isset($_POST['store'])) {
$_SESSION['store'] = 1;
$session->storeCookie();
} else {
$_SESSION['store'] = 0;
}
$location = 'Location: ';
if ($row['user_type'] == "customer") {
$location .= '/account.php';
} elseif ($row['user_type'] == "admin") {
$location .= '/admin/account.php';
} elseif ($row['user_type'] == "employee") {
$location .= '/employee/account.php';
}
header($location, true);
} else {
$msg = "Please Enter Correct E-mail OR Password.";
}
}
Thanks for your input!

Symfony doctrine batch processing not working

Maybe anyone can have a look:
I have a function and using batch to process bulk data to doctrine, but it not seems working, because nothing is inserted to database, but if i flush() every element, everything is working
any ideas why?
private function insertData($linesInFile, $output)
{
$google = $this->getContainer()->get('google.books');
$amazon = $this->getContainer()->get('amazon.books');
$em = $this->getContainer()->get('doctrine.orm.entity_manager');
$number = 1;
$batchSize = 5;
foreach ($linesInFile as $string) {
$string = preg_split('/isbn_13/', $string);
$book = new Book();
$isbn = new Isbn();
if (isset($string[1])) {
$value = str_split($string[1], 23);
$isbnValue = preg_replace('/\D/', '', $value[0]);
$isbn->setIsbn($isbnValue);
$book = $google->getBookByIsbn($isbn);
if (null == $book->getIsbn()) {
$book = $amazon->getBookByIsbn($isbn);
}
$pages = $book->getPages();
$image = $book->getCover();
$about = $book->getAbout();
if ($about !== "") {
if ($image !=="") {
$em->persist($book);
if (($number % $batchSize) === 0) {
$em->flush();
$em->clear();
}
$output->writeln($isbnValue);
$number++;
}
}
}
}
$em->flush();
$em->clear();
return $number;
}
}
So, my code is good, it was some bug in google API and items were not persisted properly.

Drupal and netForum: Creating a User

I am trying to create a user in netForum from a Drupal Webform.
Using a webform hook, I am calling two functions to take a users email address and first and last name, and create a netforum account when a user submits basic webforms.
However, the form times out when I hit submit, and the watchdog error from Netforum is 'could not fetch http headers'. Have I done something wrong in my implementation? I keep getting a timeout.
http://wiki.avectra.com/XWeb:WEBWebUserCreate
function inclind_form_webform_submission_insert($node, $submission) {
// find the email address in the form
$form_fields = $node->webform['components'];
foreach ($form_fields as $key => $value) {
$arguments = array();
$response = '';
if ($value['type'] == 'email') {
$arguments = array(
'emailToMatch' => $submission->data[$key]['value'][0]
);
$response = netforum_xweb_request('WEBWebUserFindUsersByEmail', $arguments, NULL);
if (!isset($response) || $response->{#attributes}['recordResult'] == 0) {
inclind_form_create_netforum_user($form_fields, $submission);
}
}
}
return;
}
/*
* Create a user in netForum based on form data
*
* #param $form_fields
* The form structure passed in from inclind_form_webform_submission_insert
* #param $submission
* The form data passed in from inclind_form_webform_submission_insert
*/
function inclind_form_create_netforum_user($form_fields, $submission) {
$arguments = array();
$arguments['oWebUser']['Individual'] = array();
$arguments['oWebUser']['Email'] = array();
$arguments['oWebUser']['Customer'] = array();
$arguments['oWebUser']['Business_Address'] = array();
$arguments['oWebUser']['Business_Phone'] = array();
$arguments['oWebUser']['Business_Phone_XRef'] = array();
$arguments['oWebUser']['Business_Fax'] = array();
$arguments['oWebUser']['Business_Fax_XRef'] = array();
foreach ($form_fields as $key => $value) {
if ($value['form_key'] == 'ind_first_name') {
$arguments['oWebUser']['Individual']['ind_first_name'] = $submission->data[$key]['value'][0];
}
if ($value['form_key'] == 'ind_last_name') {
$arguments['oWebUser']['Individual']['ind_last_name'] = $submission->data[$key]['value'][0];
}
if (strlen($arguments['oWebUser']['Individual']['ind_first_name']) && strlen($arguments['oWebUser']['Individual']['ind_last_name'])) {
$arguments['oWebUser']['Individual']['ind_full_name'] = $arguments['oWebUser']['Individual']['ind_first_name'] . ' ' . $arguments['oWebUser']['Individual']['ind_last_name'];
}
if ($value['form_key'] == 'eml_address') {
$arguments['oWebUser']['Email']['eml_address'] = $submission->data[$key]['value'][0];
$arguments['oWebUser']['Customer']['cst_web_login'] = $submission->data[$key]['value'][0];
$arguments['oWebUser']['Customer']['cst_new_password'] = user_password(20);
$arguments['oWebUser']['Customer']['cst_new_password_confirm'] = $arguments['oWebUser']['Customer']['cst_new_password'];
}
if ($value['form_key'] == 'adr_post_code') {
$arguments['oWebUser']['Business_Address']['adr_post_code'] = $submission->data[$key]['value'][0];
}
}
if (!isset($arguments['oWebUser']['Business_Address']['adr_city'])) {
$arguments['oWebUser']['Business_Address']['adr_city'] = 'Not Given';
}
if (!isset($arguments['oWebUser']['Business_Address']['adr_state'])) {
$arguments['oWebUser']['Business_Address']['adr_state'] = 'NA';
}
if (!isset($arguments['oWebUser']['Business_Address']['adr_post_code'])) {
$arguments['oWebUser']['Business_Address']['adr_post_code'] = '00000';
}
if (!isset($arguments['oWebUser']['Business_Address']['adr_country'])) {
$arguments['oWebUser']['Business_Address']['adr_country'] = 'Not Given';
}
if (!isset($arguments['oWebUser']['Business_Phone']['phn_number'])) {
$arguments['oWebUser']['Business_Phone']['phn_number'] = '000-000-0000';
}
if (!isset($arguments['oWebUser']['Business_Phone_XRef']['cph_extension'])) {
$arguments['oWebUser']['Business_Phone_XRef']['cph_extension'] = '000';
}
if (!isset($arguments['oWebUser']['Business_Fax']['fax_number'])) {
$arguments['oWebUser']['Business_Fax']['fax_number'] = '000-000-0000';
}
$response = netforum_xweb_request('WEBWebUserCreate', $arguments, '1 min');
watchdog('netforum', 'netforum user #user created', array('#user' => $arguments['oWebUser']['Email']['eml_address']), WATCHDOG_NOTICE);
}
Solved: http://drupal.org/node/866534

Resources